[英]mongodb aggregation get the total number of matched document
I have a following sample docs saved in mongogb, like: 我有以下示例文档保存在mongogb中,例如:
{
name: 'andy',
age: 19,
description: 'aaa aaa aaa'
}
{
name: 'andy',
age: 17,
description: 'bbb bbb bbb'
}
{
name: 'leo',
age: 10,
description: 'aaa aaa aaa'
}
{
name: 'andy',
age: 17,
description: 'ccc ccc ccc'
}
what the pipeline should look like to get the total number of name in each of matched sets? 要获得每个匹配集中的名称总数,管道应该是什么样子? so I can use this sum number for next pipe.
因此我可以将此总和编号用于下一个管道。 the pipeline I currently have is this:
我目前拥有的管道是这样的:
var pip = [
{
$match: { name: 'andy' }
}
]
and I want to get this result like 我想得到这样的结果
{
name: 'andy',
age: 19,
description: 'aaa aaa aaa',
total_andy: 3
}
{
name: 'andy',
age: 17,
description: 'bbb bbb bbb',
total_andy: 3
}
{
name: 'andy',
age: 17,
description: 'ccc ccc ccc',
total_andy: 3
}
I am not exactly clear as to what you want. 我不清楚你想要什么。 And i don't have enough reputation to ask for that in a comment.
而且我没有足够的声誉在评论中要求这样做。 So let me have a shot at answering.
因此,让我尝试一下。 If the answer isn't what you want, clarify the question further and we'll get to it...
如果答案不是您想要的,请进一步说明问题,我们将继续进行...
var term1group = {$group :
{'_id' : '$name'},
'total_names' : {$sum : 1},
'ageAndDescription' : {$addToSet : {'$age', '$description'}}
}
var term2unwind = {$unwind : '$ageAndDescription'}
var term3project = {$project : {
_id : 0,
'name' : '_id',
'age' : '$ageAndDescription.age',
'description' : '$ageAndDescription.description',
'total_name' : 1
}
db.collection.aggregate(term1group, term2unwind, term3project);
Haven't tested but i am hopeful this will work. 还没有测试过,但我希望它能起作用。
You just need to use a $group
and $sum
to do a simple count. 您只需要使用
$group
和$sum
进行简单计数即可。 The output won't match exactly, but you could reformat it with NodeJS easily. 输出不会完全匹配,但是您可以使用NodeJS轻松重新格式化。
You apparently want to group on the three fields shown ( name
, age
, and description
). 您显然想对显示的三个字段(
name
, age
和description
)进行分组。 To do that, just add the fields and a field reference (using $
): 为此,只需添加字段和字段引用(使用
$
):
{ $match: { name: 'andy' } },
{ $group: {
_id: { name: "$name", age: "$age", description: "$description"},
count: { $sum: 1}
}
}
To add the count of each group, include a $sum
of 1
(for each document that matches the group). 要添加每个组的计数,请添加
$sum
1
(对于与该组匹配的每个文档)。
Your output will look something like: 您的输出将类似于:
{ "_id" : { "name" : "andy", "age" : 17, "description" : "ccc ccc ccc" }, "count" : 1 }
{ "_id" : { "name" : "andy", "age" : 17, "description" : "bbb bbb bbb" }, "count" : 1 }
{ "_id" : { "name" : "andy", "age" : 19, "description" : "aaa aaa aaa" }, "count" : 3 }
If you used a projection with $project
, you could also format the output to more closely match your original request: 如果您将投影与
$project
,则还可以设置输出格式,使其与原始请求更加匹配:
{ $match: {name: 'andy' }},
{ $group: { _id: { name: "$name", age: "$age", description: "$description"} ,
count: {$sum: 1}}
},
{ $project : { name: "$_id.name", _id: 0, age: "$_id.age",
description: "$_id.description", total_andy: "$count"
}
}
Results: 结果:
{ "name" : "andy", "age" : 17, "description" : "ccc ccc ccc", "total_andy" : 1 }
{ "name" : "andy", "age" : 17, "description" : "bbb bbb bbb", "total_andy" : 1 }
{ "name" : "andy", "age" : 19, "description" : "aaa aaa aaa", "total_andy" : 3 }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.