簡體   English   中英

查找特定的數組,深入嵌套的Mongo集合中

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

我有一個嵌套的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"
}

嘗試在您的查詢中添加投影。

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