In Nodejs app we are using mongoose driver for MongoDB, a NoSql database with speed and accuracy. Mongo store data as series of JSON element call document and the collection of document is called a collection , same as table in DBMS.
According to the Mongoose doc Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s)
So we have the following Document , in which we want to populate user information . For simplicity I skipped some of the mongo
[
{ "tags": [
"5fef1f5187a69346300003a8",
"5fef1f5187a69346300003a9",
"5fef1f5187a69346300003aa"
],
"_id": "5fef1f5087a69346300003a7",
"title": "Title post",
"content": "It is a good practice to keep styles for different pages in separate style sheets. Larger project this becomes more applicable. What you do when need to combine them in another ?",
"user": "5fe4ab1e69b8d525c44ec293",
"createdAt": "2021-01-01T13:10:40.468Z",
"updatedAt": "2021-01-03T02:37:55.982Z",
"__v": 11
}
]
The document contains a list of posts with user information etc within the Tag ( not listed) . So the posts is a list of objects which should populate and inside each post have a user and list of tags object as well. Our task is to primarily populate the user object and the tags list.
We can do this right now using the find method of modal class accompanied by the populate command as follows
The tags array and the user belong to the posts path , so we have to repeat the population ( one for user and other for tags)
const tid = req.params['tid']
Tag.findById(tid).populate({
path: 'posts',
populate: {
path: 'tags'
},
populate: {
path: 'user',
select: 'name'
}
}).populate({
path: 'posts',
populate: {
path: 'tags',
select: 'tagName'
}
}).exec((err, post) => {
if (err) return handleError(err)
console.log('tagged posts are ' + post);
return res.status(200).send(post.posts)
})
Result
[
{
"tags": [
{
"_id": "5fef1f5187a69346300003a8",
"tagName": "Test"
},
{
"_id": "5fef1f5187a69346300003a9",
"tagName": "VM"
},
{
"_id": "5fef1f5187a69346300003aa",
"tagName": "JS"
}
],
"_id": "5fef1f5087a69346300003a7",
"title": "Title post",
"content": "It is a good practice to keep styles for different pages in separate style sheets. Larger project this becomes more applicable. What you do when need to combine them in another ?",
"user": {
"_id": "5fe4ab1e69b8d525c44ec293",
"name": "admin"
},
"createdAt": "2021-01-01T13:10:40.468Z",
"updatedAt": "2021-01-03T02:37:55.982Z",
"__v": 11
}
]
In the above code we had used the populate inside another populate because the each posts had user element.
Following mongoose post also deserve a good read
- How to export multiple components in Reactjs - How to render export multiple components in React
- How to interact with input in Reactjs - How to handle state and event of input element in React apps
- How to populate multiple MongoDB objects on the same path in express-node - How to populate multiple objects in MongoDB in the same path
- How to populate selected fields in a mongo document path in expressjs - How to populate object in mongo doc at a specific path with specific fields
- How to populate and display nested mongo objectin express-node route - How to populate object in express-MongoDB app
- How to render mongo object in Reactjs - How to render MongoDB object/ObjectId in React/MERN app
- How to render list of mongo objects in React component - How to render list of mongo objects in Reactjs
- How to place custom route links in Reactjs component - How to place custom links to routes/page in Reactjs
- How to filter MongoDB object list in express-node app - How to filter a mongo object list in document in express-node app
- How to fix mongo object Type error in Reactjs - How to fix TypeError/undefined in React - MERN application
Like this:
Like Loading...