Hello everyone, I'm going to demenstrate and explain the confusing topic of Mongodb/Mongoose that is "Populate( )" function.
Problem Statement :
Suppose you have 2 schema model named :
1 - Address Model
2 - User Model
Address Model:
There are 3 attributes in this i.e pincode, state, address
javascript
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const addressSchema = new Schema({
address: String,
state: String,
pincode : Number
});
const Address= mongoose.model("address", addressSchema);
module.exports = Address;
User Model:
There are 3 attributes in this i.e name, address, and designation.
javascript
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const {ObjectId} = mongoose.Schema;
const userSchema = new Schema({
name: String,
designation: String,
address: {
type : ObjectId,
ref : "address"
}
});
const User = mongoose.model("user", userSchema);
module.exports = User;
Solution :
Can you see the modification in User Model ?
The addition of :
javascript
address: {
type : ObjectId,
ref : "address"
}
makes all the changes here.
We are here not making the entry in User Model's address section but instead using address model to store the data and uses it's __id_ in User Model.
And at time of fetching the data from User Model we will POPULATE the address attribute from the Address Model.
What is Object Id ?
javascript
const {ObjectId} = mongoose.Schema;
and
javascript
type : ObjectId,
The ObjectId is the one of the data types of Mongoose, that tells the mongoose that this is referenced to another collection in MongoDb Database.
After importing, it is used along with ref.
What is ref ?
Now, ObjectId is used along with ref.
Ref tells the Mongoose that in which Collection the importing data is present. In our case, it's the Address Model_ that is being imported and used in the User Model. So,
javascript
address: {
type : ObjectId,
ref : "address"
}
NOTE : The value in ref the same as the
javascript
const Address= mongoose.model("address", addressSchema);
in Address Model_.
At last using POPULATE ( )
So, now it's time to fetch the data from the User Model and at time of fetching, filling the address attribute of User Model with data from the Address Model.
Let's suppose,
the __id_ of data in address model is 100.
So, at time of entering data into User Model, pass the __id_ i.e 100 to the address attribute of User Model.
javascript
User.find({}).populate("address").exec((err, result) => {
if(err){
return res.json({error : err})
}
res.json({result : result})
});
.populate("address") will fill the data coming from User.find({}) and find the __id_ present in (in our case __id_ is 100), and find that in Address Model and take that data and fill into User Model.
Thank You
Thank you, for being patient and reading till last, Hope you find it usefull. 🙂🙂🙂
My Portfolio : https://anujportfolio.herokuapp.com/
My Github : https://github.com/singhanuj620
My Linkedin : https://www.linkedin.com/in/anuj-singh-007/
Feedback is always welcomed. 🤗🤗
Top comments (1)
Thank you.