I am trying to model a database for a custom blog website using mongoDB. I have following collections
User collection:
[{
"_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"first_name": "xxx1",
"last_name": "xxx1",
"email": "xxx@gmail.com"
},{
"_id": {
"$oid": "5ed15463f43c335cb2e9ef74"
},
"first_name": "xxx2",
"last_name": "xxx2",
"email": "xxx2@gmail.com"
},{
"_id": {
"$oid": "5ed15463f43c335cb2e9ef75"
},
"first_name": "xxx3",
"last_name": "xxx3",
"email": "xxx3@gmail.com"
},{
"_id": {
"$oid": "5ed15463f43c335cb2e9ef76"
},
"first_name": "xxx4",
"last_name": "xxx4",
"email": "xxx4@gmail.com"
},{
"_id": {
"$oid": "5ed15463f43c335cb2e9ef77"
},
"first_name": "xxx3",
"last_name": "xxx3",
"email": "xxx3@gmail.com"
},{
"_id": {
"$oid": "5ed15463f43c335cb2e9ef78"
},
"first_name": "xxx4",
"last_name": "xxx4",
"email": "xxx4@gmail.com"
}]
Post collection: I have created embeded document called Comments and likes and used reference field of user_id from User collection.
[{
"_id": {
"$oid": "5f232c18a93ce29203b3f2f6"
},
"post_created_user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"post_date": {
"$date": "2020-07-30T20:22:48.839Z"
},
"post_content": "Content 1",
"post_likes": [
{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef75"
}
},
{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef76"
}
}
],
"post_comments": [
{
"comment_id": {
"$oid": "5f2850966efb9d4e803b2030"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"comment_content": "Test comment1",
"comment_date": {
"$date": "2020-08-03T17:59:50.132Z"
}
},
{
"comment_id": {
"$oid": "5f2850ff6efb9d4e803b2033"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef74"
},
"comment_content": "test comments 1",
"comment_date": {
"$date": "2020-08-03T18:01:35.008Z"
}
}
]
},{
"_id": {
"$oid": "5f232c18a93ce29203b3f2f7"
},
"post_created_user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"post_date": {
"$date": "2020-07-30T20:22:48.839Z"
},
"post_content": "content 2",
"post_likes": [
{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef77"
}
},
{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef78"
}
}
],
"post_comments": [
{
"comment_id": {
"$oid": "5f2850966efb9d4e803b2030"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"comment_content": "Test comment1",
"comment_date": {
"$date": "2020-08-03T17:59:50.132Z"
}
},
{
"comment_id": {
"$oid": "5f2850ff6efb9d4e803b2033"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef74"
},
"comment_content": "test comments 1",
"comment_date": {
"$date": "2020-08-03T18:01:35.008Z"
}
}
]
}]
Desire output:
{
"_id": {
"$oid": "5f232c18a93ce29203b3f2f6"
},
"post_created_user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"post_date": {
"$date": "2020-07-30T20:22:48.839Z"
},
"post_content": "Content 1",
"post_likes": [{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
user_info : {
"first_name": "xxx1",
"last_name": "xxx1",
"email": "xxx@gmail.com"
}, {
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef74"
},
user_info : {
"first_name": "xxx2",
"last_name": "xxx2",
"email": "xxx2@gmail.com"
}],
"post_comments": [{
"comment_id": {
"$oid": "5f2850966efb9d4e803b2030"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"comment_content": "Test comment1",
"comment_date": {
"$date": "2020-08-03T17:59:50.132Z"
},
user_info : {
"first_name": "xxx1",
"last_name": "xxx1",
"email": "xxx@gmail.com"
}
}, {
"comment_id": {
"$oid": "5f2850ff6efb9d4e803b2033"
},
"user_id": {
"$oid": "5ed15483f43c335cb2e9ef74"
},
"comment_content": "test comments 1",
"comment_date": {
"$date": "2020-08-03T18:01:35.008Z"
}
user_info : {
"first_name": "xxx2",
"last_name": "xxx2",
"email": "xxx2@gmail.com"
}
}]
}
Look up I tried on Post collection:
[
{
'$unwind': {
'path': '$post_likes',
'preserveNullAndEmptyArrays': True
}
}, {
'$unwind': {
'path': '$post_comments',
'preserveNullAndEmptyArrays': True
}
}, {
'$lookup': {
'from': 'users_details',
'localField': 'post_likes.user_id',
'foreignField': '_id',
'as': 'post_likes.user_info'
}
}, {
'$lookup': {
'from': 'users_details',
'localField': 'post_comments.user_id',
'foreignField': '_id',
'as': 'post_comments.user_info'
}
}, {
'$unwind': {
'path': '$post_likes.user_info',
'preserveNullAndEmptyArrays': True
}
}, {
'$unwind': {
'path': '$post_comments.user_info',
'preserveNullAndEmptyArrays': True
}
}, {
'$group': {
'_id': '$_id',
'post_created_user_id': {
'$first': '$post_created_user_id'
},
'post_content': {
'$first': '$post_content'
},
'post_likes': {
'$push': '$post_likes'
},
'post_comments': {
'$push': '$post_comments'
}
}
}
]
Getting the following output with duplicates val.
[{
"_id": {
"$oid": "5f232c18a93ce29203b3f2f6"
},
"post_created_user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"post_content": "Content 1",
"post_likes": [
{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef75"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef75"
},
"first_name": "xxx3",
"last_name": "xxx3",
"email": "xxx3@gmail.com"
}
},
{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef75"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef75"
},
"first_name": "xxx3",
"last_name": "xxx3",
"email": "xxx3@gmail.com"
}
},
{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef76"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef76"
},
"first_name": "xxx4",
"last_name": "xxx4",
"email": "xxx4@gmail.com"
}
},
{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef76"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef76"
},
"first_name": "xxx4",
"last_name": "xxx4",
"email": "xxx4@gmail.com"
}
}
],
"post_comments": [
{
"comment_id": {
"$oid": "5f2850966efb9d4e803b2030"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"comment_content": "Test comment1",
"comment_date": {
"$date": "2020-08-03T17:59:50.132Z"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"first_name": "xxx1",
"last_name": "xxx1",
"email": "xxx@gmail.com"
}
},
{
"comment_id": {
"$oid": "5f2850ff6efb9d4e803b2033"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef74"
},
"comment_content": "test comments 1",
"comment_date": {
"$date": "2020-08-03T18:01:35.008Z"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef74"
},
"first_name": "xxx2",
"last_name": "xxx2",
"email": "xxx2@gmail.com"
}
},
{
"comment_id": {
"$oid": "5f2850966efb9d4e803b2030"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"comment_content": "Test comment1",
"comment_date": {
"$date": "2020-08-03T17:59:50.132Z"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"first_name": "xxx1",
"last_name": "xxx1",
"email": "xxx@gmail.com"
}
},
{
"comment_id": {
"$oid": "5f2850ff6efb9d4e803b2033"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef74"
},
"comment_content": "test comments 1",
"comment_date": {
"$date": "2020-08-03T18:01:35.008Z"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef74"
},
"first_name": "xxx2",
"last_name": "xxx2",
"email": "xxx2@gmail.com"
}
}
]
},{
"_id": {
"$oid": "5f232c18a93ce29203b3f2f7"
},
"post_created_user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"post_content": "content 2",
"post_likes": [
{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef77"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef77"
},
"first_name": "xxx3",
"last_name": "xxx3",
"email": "xxx3@gmail.com"
}
},
{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef77"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef77"
},
"first_name": "xxx3",
"last_name": "xxx3",
"email": "xxx3@gmail.com"
}
},
{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef78"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef78"
},
"first_name": "xxx4",
"last_name": "xxx4",
"email": "xxx4@gmail.com"
}
},
{
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef78"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef78"
},
"first_name": "xxx4",
"last_name": "xxx4",
"email": "xxx4@gmail.com"
}
}
],
"post_comments": [
{
"comment_id": {
"$oid": "5f2850966efb9d4e803b2030"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"comment_content": "Test comment1",
"comment_date": {
"$date": "2020-08-03T17:59:50.132Z"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"first_name": "xxx1",
"last_name": "xxx1",
"email": "xxx@gmail.com"
}
},
{
"comment_id": {
"$oid": "5f2850ff6efb9d4e803b2033"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef74"
},
"comment_content": "test comments 1",
"comment_date": {
"$date": "2020-08-03T18:01:35.008Z"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef74"
},
"first_name": "xxx2",
"last_name": "xxx2",
"email": "xxx2@gmail.com"
}
},
{
"comment_id": {
"$oid": "5f2850966efb9d4e803b2030"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"comment_content": "Test comment1",
"comment_date": {
"$date": "2020-08-03T17:59:50.132Z"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef73"
},
"first_name": "xxx1",
"last_name": "xxx1",
"email": "xxx@gmail.com"
}
},
{
"comment_id": {
"$oid": "5f2850ff6efb9d4e803b2033"
},
"user_id": {
"$oid": "5ed15463f43c335cb2e9ef74"
},
"comment_content": "test comments 1",
"comment_date": {
"$date": "2020-08-03T18:01:35.008Z"
},
"user_info": {
"_id": {
"$oid": "5ed15463f43c335cb2e9ef74"
},
"first_name": "xxx2",
"last_name": "xxx2",
"email": "xxx2@gmail.com"
}
}
]
}]
Can someone help me to construct the aggregation?
Take a look step by step,
$unwind
post_comments because its array and we need to lookup by single comment db.posts.aggregate([
{
$unwind: "$post_comments"
},
$lookup
Its correct that you have prepared {
$lookup: {
from: "users_details",
localField: "post_comments.user_id",
foreignField: "_id",
as: "post_comments.user_info"
}
},
{
$project: {
"post_comments.user_info._id": 0
}
},
$unwind
post_comments.user_info
because its an array and we need only single user_info object {
$unwind: {
path: "$post_comments.user_info"
}
},
$group
by _id
because above unwind separated the all documents, and we need to combine here {
$group: {
_id: "$_id",
post_created_user_id: {
$first: "$post_created_user_id"
},
post_date: {
$first: "$post_date"
},
post_content: {
$first: "$post_content"
},
post_comments: {
$push: "$post_comments"
}
}
}
])
post_comments
to post_likes
and change in $group to push post_likes
, you can check the below playground.You can combine the above query part as they are in sequence, separated for the explanation purpose.
Playground: https://mongoplayground.net/p/GzVvEbPNwkA
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.