mongo 聚合后统计数量
db.getCollection("test").aggregate([
{
$lookup:
{
from: "目标集合",
localField: "当前集合连接的属性",
foreignField: "目标集合连接的属性",
as: "inventory_docs" // 这是将对应的目标集合放到了这里
}
},
{
$match: { // 这是过滤条件
field1: "field1",
"inventory_docs": {$elemMatch:{"name":"name1"}} // 筛选目标集合中的属性
}
},
{
$group: {_id: null, count: {$sum:1}} // 统计共有几条记录
},
{
$project : {_id: 0} // 将_id剔除出结果
}
])