Step 10

A Mongoose Model is a class. You can use it to construct objects using the new operator:

const user = new User({
  username: faker.internet.userName(),
  password: faker.internet.password(),
});

console.log(user);

You can then "save" the object in the database (which is connected to mongoose):

user.save((err, user) => {
  if (err) console.log(err);
  console.log(user);
});

So the Mongoose Model can be used to create objects of certain type in your application, as well as to connect to the database and handle the persistence mechanism.

Aside: I take issue with the design of Mongoose; it seems to violate the "Single Responsibility Principle" as the "data model" is fused with "data access object". At any rate, this is how Mongoose models work. In fact, there are several other methods attached to the "model" that allows you to perform various database queries.

For example, to retrieve all the "users" you can use the find method:

User.find()
  .then((users) => console.log(users))
  .catch((err) => console.log(err));

Notice find is invoked through User the model we created for constructing user documents (not the object user).

To find a specific user given their ID, you can use findById:

User.findById("5f9a1d156e848f06c458f5f4")
  .then((user) => console.log(user))
  .catch((err) => console.log(err));

The 5f9a1d156e848f06c458f5f4 is an ID of one of the users in my database. You must use an ID for one of your user documents.