For example, I have two collections, post
and comment
.
The query code is
post.aggregate([
{
$lookup: {
from: 'comment',
localField: '_id',
foreignField: 'postId',
as: 'post_comment'
},
}
])
The result is like
{
_id: new ObjectId('665fa2e22ed07dbfa654cef1'),
post_comment: [
{
_id: new ObjectId('665fb02cf3176331944fbbee'),
postId: new ObjectId('665fa2e22ed07dbfa654cef1'),
text: 'comment 1'
}
]
}
But I don’t need the details of the comments, I only need the number of comments of each post.
The desired result is like
{
_id: new ObjectId('665fa2e22ed07dbfa654cef1'),
post_comment: [
{
count: 1 // newly changed.
}
]
}
Or simply
{
_id: new ObjectId('665fa2e22ed07dbfa654cef1'),
comment_count: 1 // newly changed.
}
Could anyone teach me how to do it?
Thanks!