Mongoose "Populate()" in most simple way | How to import a collection into another schema in Mongodb
Tags: mongodb, mongoose, node, javascript
Published: 2021-04-18 10:55:37

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})
	});
```

![Mongoose Populate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozxvr1f8bby3un9t9xch.png)

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