DEV Community

Cover image for Mongoose "Populate()" in most simple way | How to import a collection into another schema in Mongodb
Anuj Singh
Anuj Singh

Posted on • Updated on

Mongoose "Populate()" in most simple way | How to import a collection into another schema in Mongodb

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

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;
Enter fullscreen mode Exit fullscreen mode

User Model:
There are 3 attributes in this i.e name, address, and designation.

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;
Enter fullscreen mode Exit fullscreen mode

Solution :

Can you see the modification in User Model ?
The addition of :

address: {
        type :  ObjectId,
        ref :  "address"
    }
Enter fullscreen mode Exit fullscreen mode

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 ?

const {ObjectId} = mongoose.Schema; 
Enter fullscreen mode Exit fullscreen mode

and

type :  ObjectId,
Enter fullscreen mode Exit fullscreen mode

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,

address: {
        type :  ObjectId,
        ref :  "address"
    }
Enter fullscreen mode Exit fullscreen mode

NOTE : The value in ref the same as the

const  Address= mongoose.model("address", addressSchema);
Enter fullscreen mode Exit fullscreen mode

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.

User.find({}).populate("address").exec((err, result) => {
    if(err){
        return  res.json({error :  err})
    }
    res.json({result :  result})
    });
Enter fullscreen mode Exit fullscreen mode

Mongoose Populate

.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. 🤗🤗

Discussion (1)

Collapse
jmturaev profile image
JMTuraev

Thank you.