I have this users and posts collection with embedded docs comments
and replies
:
- users
{
_id:34
name:"Palestine"
}
- Posts
{
_id:"1234",
body:" hello ! I love mongodb, but its hard",
likes:["34"],
comments:{
_id:"3453",
body:"me I don't like mongodb i like sql "
likes:["34"],
replies:{
_id:"2345",
body:"both of them are great"
likes:["34"],
}
}
}
The question is that, i want to aggregate into the posts and get all the them, after that i want to append new key value to both post, comments, replies, witch gonna specify if the user liked the post or not ex: liked:true
Note: I have the authenticated user id ready . I want check if the user Id exist in the likes
docs on each sub trees (posts, comments, replies )
To be more specific about this question this is my expected humble result :
{
_id:"1234",
body:" hello ! I love mongodb, but its hard",
liked:true,
likesCount:1
comments:{
_id:"3453",
body:"me I don't like mongodb i like sql "
liked:true,
likesCount:1
replies:{
_id:"2345",
body:"both of them are great"
liked:true,
likesCount:1
}
}
}
Note : If you can help to query upto the comments its okay for me :
Result expected :
{
_id:"1234",
body:" hello ! I love mongodb, but its hard",
liked:true,
likesCount:1
comments:{
_id:"3453",
body:"me I don't like mongodb i like sql "
liked:true,
likesCount:1
}
}
This is what I tried :
const result = await PostModel.aggregate([
{
$project: {
likesCount: { $size: "$likes" },
commentsCount: { $size: "$comments" },
liked: { $in: [ID(uid), "$likes"] },
likes: 1,
||
||
\/
Note: i dont know if i need to `$group` first or `$unwind`
................................................
}
}
…
I have a hard time finding how to perform the next step