基于嵌套密钥mongodb的计数
如何根据xTag键在文档上进行计数 我试过这个,但它不能提供我的实际计数基于嵌套密钥mongodb的计数,mongodb,aggregation-framework,Mongodb,Aggregation Framework,如何根据xTag键在文档上进行计数 我试过这个,但它不能提供我的实际计数 db.collection.find({ "products.xTag": { $exists: false } }).count(); 当您使用$exist:true运行时,我希望结果为1 当您使用$exist:false运行时,我希望得到结果3 游乐场: 结构: [ { "item": 1, "products": [
db.collection.find({
"products.xTag": {
$exists: false
}
}).count();
当您使用$exist:true运行时,我希望结果为1
当您使用$exist:false运行时,我希望得到结果3
游乐场:
结构:
[
{
"item": 1,
"products": [
{
"name": "xyz",
"xTag": 32423
},
{
"name": "abc"
}
]
},
{
"item": 2,
"products": [
{
"name": "bob",
},
{
"name": "foo"
}
]
}
]
使用find()不可能,您可以使用
$unwind
解构产品
阵列
$match
您的条件
$count
文档总数
db.collection.aggregate([
{ $unwind: "$products" },
{ $match: { "products.xTag": { $exists: false } } },
{ $count: "count" }
])