VOOZH about

URL: https://www.geeksforgeeks.org/mongodb/mongoose-document-model-prototype-model-api/

⇱ Mongoose Document Model.prototype.model() API - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Mongoose Document Model.prototype.model() API

Last Updated : 28 Apr, 2025

The Mongoose Document API Model.prototype.model() method of the Mongoose API is used on the Document model. It allows to get the model instance. We can call this method on any model object and by providing another model name to the method parameter we will get the new instance of that model. Let us understand the model() method using an example.

Syntax:

document.model( model );

Parameters: This method accepts a single parameter as discussed below:

  • model: It is used to specify the name of the model.

Return Value: This method return the new instance of model, we send as a parameter to the method.

Setting up Node.js application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this: 

👁 Image
 

Database Structure: The database structure will look like this, the following documents are present in the collection.

👁 Image
 

Example 1: In this example, we are illustrating the functionality of model() method. We have defined a DUMMYMODEL, using the object of dummymodel we are creating new instance of Customer model and at the end fetching one record from the database.

Filename: app.js

Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

[
 {
 _id: new ObjectId("639ede899fdf57759087a655"),
 name: 'Chintu',
 address: 'IND',
 orderNumber: 9,
 __v: 0
 }
]

Example 2: In this example, we are illustrating the functionality of model() method. At the end, we are comparing the newly instantiated object with original Customer model. 

Filename: app.js

Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

true

Reference: https://mongoosejs.com/docs/api/model.html#model_Model-model

Comment

Explore