Model()

Parameters
  • doc «Object» values for initial set

  • optional «[fields]» object containing the fields that were selected in the query which returned this document. You do not need to set this parameter to ensure Mongoose handles your query projection.

  • [skipId=false] «Boolean» optional boolean. If true, mongoose doesn’t add an _id field to the document.

Inherits:

A Model is a class that’s your primary tool for interacting with MongoDB. An instance of a Model is called a Document.

In Mongoose, the term “Model” refers to subclasses of the mongoose.Model class. You should not use the mongoose.Model class directly. The mongoose.model() and connection.model() functions create subclasses of mongoose.Model as shown below.

Example:

  1. // `UserModel` is a "Model", a subclass of `mongoose.Model`.
  2. const UserModel = mongoose.model('User', new Schema({ name: String }));
  3. // You can use a Model to create new documents using `new`:
  4. const userDoc = new UserModel({ name: 'Foo' });
  5. await userDoc.save();
  6. // You also use a model to create queries:
  7. const userFromDb = await UserModel.findOne({ name: 'Foo' });