MongoDB,查找字段到数组

时间:2014-07-22 21:24:19

标签: mongodb find

我的藏品中有这些物品

{
user: data,
somestuff: [somedata ...], 
terminals: [ {
   label: data,
   details: [{more content}, {}, ...]
   }]
}

我会使用'find'来提取特定终端'标签'的“详细信息”字段 我知道有一种简单的方法可以通过以下方式获得“终端”阵列:

collection.find({_id: "..."}, {_id:0, terminals: 1})

回归

{ terminals: [ {
   label: data,
   details: [{more content}, {}, ...]
   }]
}

我试过

collection.find({ "terminals.label": data }, { _id: 0, "terminals.$.details": 1 })

正如edirican建议的那样 它几乎可以工作,但它返回的结构与之前相同,只是终端列表只包含标记文档

我期望的结果是从终端

中提取的详细信息列表
{ details: [{more content}, {}, ...] }

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

使用位置($)投影。

http://docs.mongodb.org/manual/reference/operator/projection/positional/

collection.find({ "terminals.label": 2 }, { _id: 0, "terminals.$.details": 1 })
相关问题