我在mongo集合中有以下结构
[{
"_id" : ObjectId("543fa67e9672ec37ebe3d028"),
"customerId" : "5429aed703646b52def71736",
"tagName" : "abc",
"tagMembers" : [
"111",
"112",
"123"
]
},
{
"_id" : ObjectId("543fa67e9672ec37ebe3d029"),
"customerId" : "5429aed703646b52def71736",
"tagName" : "pqr",
"tagMembers" : [
"111",
"112",
"123"
]
}]
我想计算每个tagMember的计数。
如何使用mongo ??
计算它是否可以在不使用聚合的情况下进行计数?
答案 0 :(得分:0)
使用聚合框架和$size运算符:
> db.test.aggregate([
{ "$project" : { "_id" : 0, "tagMembersLength" : { "$size" : "$tagMembers" } } }
])
{ "tagMembersLength" : 3 }
{ "tagMembersLength" : 3 }