db.sample.find():
{_id:1, locations: [{vname: "edd", distr: "prakasam"}, {vname: "vij", distr: "krishna"}, {vname: "kakani", distr: "guntur"}]};
我想找到vname: "vij"
我的结果可能是
{_id: 1, locations: [{vname: "edd", distr: "prakasam"}, {vname: "kakani", distr: "guntur"}]};
我尝试了以下查询,但无法帮助我。
db.sample.find({'locations.$.vname': {$ne: 'vij'}})
它将返回整个文档,但我需要排除此文档。
任何帮助都很明显。
答案 0 :(得分:2)
使用$redact
。当$$PRUNE
数组与locations
匹配为vname
时,vij
db.sample.aggregate({
$redact: {
$cond: [{
$eq: ["$vname", "vij"]
},
"$$PRUNE",
"$$DESCEND"
]
}
});
答案 1 :(得分:0)
试试这个:
> db.sample.find({"locations.vname": {$ne: "vij"}})
答案 2 :(得分:0)
由于location
是架构中的对象数组,因此您无法从结果中排除特定对象。您可以从结果中排除整个db.sample.aggregate([
{
"$match" :{"_id":1}
},
{
"$unwind" :"$locations"
},
{
"$match" :{
"locations.vname":{"$ne": "vij"}
}
}
])
字段。但是,您可以选择在集合上运行聚合。
{ "_id" : 1, "locations" : { "vname" : "edd", "distr" : "prakasam" } }
{ "_id" : 1, "locations" : { "vname" : "kakani", "distr" : "guntur" } }
这将以此形式返回数据。
<ESB_HOME>/ repository/components/lib