【发布时间】:2015-04-09 00:14:17
【问题描述】:
我有一个包含数组的文档。像这样:
"_id" : ObjectId("55101f81e4b07caf8554b9b1"),
"myId" : "1222222",
"isDelayed" : false,
"status" : "BALLS",
"yellow" : false,
"white" : true,
"people" : [
{
"peopleId" : 222222,
"bc" : 0,
"status" : "live",
"fc" : 1,
"tc": 4,
"rc": "yellow"
},
{
"peopleId" : 33312,
"bc" : 0,
"status" : "live",
"fc" : 1,
"tc": 4,
"rc": "yellow"
},
...
我有一个如下所示的 mongo 查询,在集合 mycoll 中,如果 myId=1.222 和在人员数组中,如果 people.peopleId=1123 返回第一个匹配项:
db.getCollection('mycoll').find(
{myId:'1.222',
people: { $elemMatch: { peopleId: 1123 }
}
},{"people.$": 1 }).pretty();
结果包括数组中人员条目中的所有字段:
"people" : [
{
"peopleId" : 1122,
"bc" : 0,
"status" : "live",
"fc" : 1,
"tc": 4,
"rc": "yellow"
},
如何过滤仅从内部数组中的匹配条目返回所需字段的回复,例如"status"?我可以为外部文档生成过滤器,但不能为数组元素中的字段生成过滤器。
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework database nosql