我们的网站结构如下:
{
"_id" : ObjectId("52b3833bd3e98582d2bfb628"),
"author" : {
"name" : "Graydon Hoare",
"email" : "..."
},
"title" : "Why Rust ditched pure functions",
"body" : "sth",
"starred" : 105,
"comments" : [
{
"user" : "hypster",
"upVotes" : 30,
"downVotes" : 2,
"text" : "....",
"replies" : [
{
"user" : "homoiconic",
"upVotes" : 15,
"downVotes" : 0,
"text" : "Here comes the Big Boss, Hu! Simon Peyton-Jones."
}
我想要的是计算每个用户的评论数(也可以在回复中找到),并检查它是否大于1,然后将其检索。
我已经实现了以下代码:
db.links.aggregate([
{
$unwind: "$comments.user"
},
{
$unwind: "$comments.replies.user"
},
{
$group: {
_id: "$comments.user",
totalComments: {$sum: "$name"},
count: {$sum: 1}
}
},
])
换句话说,我们要计算用户在每个帖子中留在站点中的评论数(如果该评论数大于一个)。