Summary:
Current Data:
db.posts.find()
[
{ authorId: 1,
text: "lorem",
likes: 22
comments: [
{ commentatorId: 1, commentText: "cool"},
{ commentatorId: 2, commentText: "nice"},
{ commentatorId: 8, commentText: "trash"}
]
},
{ authorId: 2,
text: "ipsum",
likes: 33,
comments: [
{ commentatorId: 1, commentText: "cool"},
{ commentatorId: 1, commentText: "nice"},
{ commentatorId: 3, commentText: "trash"}
]
},
{ authorId: 3,
text: "ipsum",
likes: 44,
comments: [
{ commentatorId: 2, commentText: "cool"},
{ commentatorId: 2, commentText: "nice"},
{ commentatorId: 3, commentText: "trash"}
]
},
{ authorId: 1,
text: "ipsum",
likes: 55,
comments: [
{ commentatorId: 1, commentText: "cool"},
{ commentatorId: 2, commentText: "nice"},
{ commentatorId: 3, commentText: "trash"}
]
},
]
Desired Outcome:
{
1: {posts: 2, likes: 77, comments: 4},
2: {posts: 1, likes: 33, comments: 4},
3: {posts: 1, likes: 44, comments: 3},
8: {posts: 0, likes: 0, comments: 1},
}
The challenge here is to aggregate data based on authorId and commentatorId fields which are linked to another collection (named "users"). Every user should be included in the result regardless of whether they have posted any content. The count for comments should include all posts, not just ones where authorId is equal to commentatorId.