n次我有一个MongoDB的集合world
在以下格式的文档:如何查询文件,其中阵列有一个字段超过MongoDB中
{
_id : ObjectId("4e8ae86d08101908e1000001"),
country : [
{
state: "Newyork",
type: 1
},
{
state: "California",
type: 1
},
{
state: "Texas",
type: 2
}
]
}
我们可以很容易地得到具有文档四在arrary更指出:
db.world.find({'country.4': {$exists: true} })
但我怎么能得到的文件,其中具有type: 1
四个或更多状态的国阵?
此外,我想在查询中避免$where
运算符。
编辑1
回答布雷克七权似乎对我,但是当我尝试做反向即;得到的文件与少超过n区,然后我得到错误的结果:
下面是该查询:
db.world.aggregate([
{ "$redact": {
"$cond": {
"if": {
"$lte": [
{ "$size": { "$setDifference": [
{ "$map": {
"input": "$country",
"as": "el",
"in": {
"$cond": {
"if": { "$eq": [ "$$el.type", 769 ] },
"then": "$$el",
"else": false
}
}
}},
[false]
]}},
4
]
},
"then": "$$KEEP",
"else": "$$PRUNE"
}
}}
]);