简体   繁体   English

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

[英]Find specific array, deep in a nested Mongo collection

I have a nested Mongo collection which looks like the following: 我有一个嵌套的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"
}

I'm trying to query using Collection.findOne("BaiD76JR3gZFPKchn",) , to return an object in "activities" using 2 field "id:a2" AND "type:image", so I end up with : 我正在尝试使用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"
}

Try adding projection into your query. 尝试在您的查询中添加投影。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM