I have two documents in two separate collections. Say, I have document one in post
collection and the document is
_id: ObjectId('63e23a258224d2ef3fea4a95')
title: 'Post title'
and my other document in comment
collection and the document is
_id: ObjectId('63e49e6ef73fe8e6fba1461b')
userId: ObjectId('xxx')
postId: ObjectId('63e23a258224d2ef3fea4a95')
comment: 'This is a comment'
In my comment
collection there are lots of document that has same postId. I also have another collections called users
. Now, I want to get something like
_id: ObjectId('63e23a258224d2ef3fea4a95')
title: 'Post title'
comments: [{
_id: ObjectId('63e49e6ef73fe8e6fba1461b')
userId: {
name: 'John'
age: 50
}
postId: ObjectId('63e23a258224d2ef3fea4a95')
comment: 'This is a comment'
}, {
_id: ObjectId('63e49e6ef73fe8e6fba1461c')
userId: {
name: 'John'
age: 50
}
postId: ObjectId('63e23a258224d2ef3fea4a95')
comment: 'This is a comment'
}]
How can I achieve this? I’m using nodejs
and official mongodb
package
I tried searching on Google using aggregate, group keywords. But didn’t find solution that I’m looking for.
2
Answers
You can implement this functionality inside your backend routes:
This can be done using
$lookup
.You can try it out in this mongoDB playground.
Be sure to create an index on
comments.postId
.It might be worth pointing out that, depending on your use case, this could return a lot of data due to the 1-Many relationship between posts -> comments so you might want to consider separating the post and comment DB calls so you can paginate the comments.
If you don’t expect many comments (i.e. it’s a 1-Few relationship), you might want to consider modelling the comments as embedded documents instead of a separate collection which would make reads simpler.
Some further information on 1-Many / 1-Few schema design can be found here and here.