Mongoose Document Model.populate() API
The Mongoose Document API Model.populate() method of the Mongoose API is used on the Document model. It allows to replacement of a field in a collection with a document from another collection. We can provide a collection name as a value to a field as a reference to populate the collection and its field. Let us understand the populate() method using an example.
Syntax:
Model.populate( doc, options, callback );
Parameters: This method accepts three parameters as discussed below:
- doc: It is used to specify the document name. It could be a single document or an array of documents.
- options: It is used to configure the various properties.
- callback: It is used to specify the callback function which will be executed once the operation is done.
Return Value: This method returns the promise, which can be handled using the callback function.
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.
- Customer collection has the following documents:
- Product collection has the following documents:
Example 1: In this example, we have established a database connection using mongoose and defined two models over customerSchema, and productSchema. In the end, we are accessing populate() method over the Product model and providing the customer model as a reference to point and populate the referenced documents from both collections.
Filename: app.js
Javascript
// Require mongoose module const mongoose = require( "mongoose" ); // Set Up the Database connection let connectionObject = mongoose.createConnection(URI, { useNewUrlParser: true , useUnifiedTopology: true , }); let customerSchema = new mongoose.Schema({ name: String, address: String, orderNumber: Number, }) let Customer = connectionObject.model( "Customer" , customerSchema); let productSchema = new mongoose.Schema({ name: String, price: Number, customer: { type: mongoose.ObjectId, ref: 'Customer' }, }) let Product = connectionObject.model( "Product" , productSchema); Product.find().populate( "customer" ).then(res => { console.log(res); }); |
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("63c93f348d749df47132da0d"), name: 'Samsung TV 32', price: 18000, customer: { _id: new ObjectId("63c13b76876922405349f708"), name: 'Mivaan', address: 'IND', orderNumber: 9, __v: 0 }, __v: 0 }, { _id: new ObjectId("63c93f348d749df47132da0b"), name: 'DJI Mavic Mini 2', price: 25000, customer: { _id: new ObjectId("639ede899fdf57759087a655"), name: 'Chintu', address: 'IND', orderNumber: 9, __v: 0 }, __v: 0 }, { _id: new ObjectId("63c93f348d749df47132da0c"), name: 'iPhone 11', price: 55000, customer: { _id: new ObjectId("639ede899fdf57759087a653"), name: 'Aditya', address: 'Mumbai', orderNumber: 20, __v: 0 }, __v: 0 } ]
Example 2: In this example, we are accessing the populate() method on the Customer model. For reference, we are providing the path name to be considered as a reference to another collection.
Filename: app.js
Javascript
// Require mongoose module const mongoose = require( "mongoose" ); // Set Up the Database connection let connectionObject = mongoose.createConnection(URI, { useNewUrlParser: true , useUnifiedTopology: true , }); let customerSchema = new mongoose.Schema({ name: String, address: String, orderNumber: Number, }) let Customer = connectionObject.model( "Customer" , customerSchema); let productSchema = new mongoose.Schema({ name: String, price: Number, customer: { type: mongoose.ObjectId, ref: 'Customer' }, }) let Product = connectionObject.model( "Product" , productSchema); let products; Product.find({ name: "Samsung TV 32" }).then(res => { products = res; Customer.populate(products, { path: "customer" }).then(res => { console.log(res); }) }); |
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("63c93f348d749df47132da0d"), name: 'Samsung TV 32', price: 18000, customer: { _id: new ObjectId("63c13b76876922405349f708"), name: 'Mivaan', address: 'IND', orderNumber: 9, __v: 0 }, __v: 0 } ]
Reference: https://mongoosejs.com/docs/api/model.html#model_Model-populate
Please Login to comment...