这里的文件一个例子,我用:
{
"_id" : ObjectId("554a1f5fe36a768b362ea5c0"),
"store_state" : 1,
"services" : [
{
"id" : "XXX",
"state" : 1,
"active": true
},
{
"id" : "YYY",
"state" : 1,
"active": true
},
...
]
}
我要输出与“Y”一个新的领域,如果id为“XXX”和积极的真实,“N “在任何其他情况下。每个文档都没有“XXX”作为标识的服务元素(在这种情况下,输出“N”)。
这里是我的时刻查询:
db.stores.aggregate({
$match : {"store_state":1}
},
{ $project : {
"XXX_active": {
$cond: [ {
$and:[
{$eq:["services.$id","XXX"]},
{$eq:["services.$active",true]}
]},"Y","N"
] }
}
}).pretty()
但它总是输出"N"
为"XXX_active"
领域。
预期输出我需要的是:
{
"_id" : ObjectId("554a1f5de36a768b362e7e6f"),
"XXX_active" : "Y"
},
{
"_id" : ObjectId("554a1f5ee36a768b362e9d25"),
"XXX_active" : "N"
},
{
"_id" : ObjectId("554a1f5de36a768b362e73a5"),
"XXX_active" : "Y"
}
的可能结果的其它例子:
{
"_id" : ObjectId("554a1f5de36a768b362e7e6f"),
"XXX_active" : "Y",
"YYY_active" : "N"
},
{
"_id" : ObjectId("554a1f5ee36a768b362e9d25"),
"XXX_active" : "N",
"YYY_active" : "N"
},
{
"_id" : ObjectId("554a1f5de36a768b362e73a5"),
"XXX_active" : "Y",
"YYY_active" : "Y"
}
只有一个每个对象XXX_active
并没有重复对象,但我需要一个XXX_active
即使所有对象服务id元素“XXX”不存在。有人可以帮忙吗?
您能告诉我们预期的产量吗? – chridam
我需要的输出是: {“_id”:ObjectId(“554a1f5de36a768b362e7e6f”),“XXX_active”:“Y”} {“_id”:ObjectId(“554a1f5ee36a768b362e9d25”),“XXX_active”:“N”} { “_id”:ObjectId(“554a1f5de36a768b362e73a5”),“XXX_active”:“Y”} 每个对象只有一个XXX_active。谢谢 –