Mongoose Document Model.prototype.model() API
Last Updated :
20 Jan, 2023
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:
Database Structure: The database structure will look like this, the following documents are present in the collection.
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
Javascript
const mongoose = require( "mongoose" );
const connectionObject = mongoose.createConnection(URI, {
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const Customer = connectionObject.model(
"Customer" ,
new mongoose.Schema({
name: String,
address: String,
orderNumber: Number,
})
);
const DUMMYMODEL = connectionObject.model(
"DUMMYMODEL" ,
new mongoose.Schema({
name: String,
})
);
const doc = new DUMMYMODEL();
doc.$model( "Customer" ).find({ name: "Chintu" }).then(res => {
console.log(res);
}). catch (err => {
console.log(err);
});
|
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
Javascript
const mongoose = require( "mongoose" );
const connectionObject = mongoose.createConnection(URI, {
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const Customer = connectionObject.model(
"Customer" ,
new mongoose.Schema({
name: String,
address: String,
orderNumber: Number,
})
);
const DUMMYMODEL = connectionObject.model(
"DUMMYMODEL" ,
new mongoose.Schema({
name: String,
})
);
const doc = new DUMMYMODEL();
const newInstance = doc.$model( "Customer" )
console.log(newInstance.modelName === Customer.modelName);
|
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...