在嵌套的Mongo集合中深入查找特定数组

时间:2017-10-26 16:27:54

标签: javascript mongodb meteor meteor-collections

我有一个嵌套的Mongo集合,如下所示:

{
  "_id": "BaiD76JR3gZFPKchn",
  "ownerAdmin": "NsgdNHh84XrBJALAK",
  "activities": [
    {
      "id": "a1",
      "email": "itsarmin@gmail.com",
      "type": "image",
      "description": "Another one",
      "createdAt": "2017-10-25T21:39:44.769Z",
      "pins":
      "action": "https://www.dropbox.com/as"
    },
    {
      "id": "a2",
      "email": "itsarmin@gmail.com",
      "type": "image",
      "description": "Comment This image For me",
      "createdAt": "2017-10-25T18:42:00.484Z",
      "action": "https://www.dropbox.com/s/bty2ds"
    },
    {
      "id": "a3"
      "email": "armin@four1five.com",
      "type": "progress",
      "description": "dasd",
      "createdAt": "2017-10-24T17:28:11.748Z",
      "action": 50
    }
  ],
  "createdAt": "2017-10-24T17:27:54.828Z"
}

我正在尝试使用Collection.findOne("BaiD76JR3gZFPKchn",)进行查询,使用2字段“id:a2”和“type:image”返回“活动”中的对象,因此我最终得到:

{
  "id": "a2",
  "email": "itsarmin@gmail.com",
  "type": "image",
  "description": "Comment This image For me",
  "createdAt": "2017-10-25T18:42:00.484Z",
  "action": "https://www.dropbox.com/s/bty2ds"
}

1 个答案:

答案 0 :(得分:0)

尝试在您的查询中添加投影。

db.collection.findOne( { _id: "BaiD76JR3gZFPKchn" },
               { activities: { $elemMatch: { id: "a2",type:"image"} }, _id: 0 })