在Mongodb上查询对象的深层嵌套数组

时间:2020-05-23 08:39:11

标签: arrays mongodb object mongodb-query

我有一个包含许多数组对象的Mongodb文档,这是摘录:

{ 
"_id" : ObjectId("51df07b094c6acd67e492f42"), 
"maint_logbooks" : [
    {
        "_id" : NumberInt(2), 
        "logbook_number" : NumberInt(242146), 
        "date" : "2020-04-14T04:00:00Z", 
        "flight_log" : "xxxxx", 
        "flight_num" : "121212", 
        "rcca" : [
            {
                "id" : NumberInt(1), 
                "stn" : "MMMX", 
                "crew/mma" : "MAA", 
                "rep_by" : "Phillip Wood", 
                "discrepancie" : "COM 2 does not transmit.", 
                "ATA" : "xxxx", 
                "basis for deferreal" : "", 
                "name" : "Elle Snider", 
                "licence_num" : "1213123r4", 
                "ca" : "lorem ipsun", 
                "oil" : {
                    "L" : "n/a", 
                    "R" : "n/a", 
                    "APU" : "n/a"
                }
            }, 
            {
                "id" : NumberInt(2), 
                "stn" : "MMMX", 
                "crew/mma" : "MAA", 
                "rep_by" : "Phillip Wood", 
                "discrepancie" : "COM 2 does not transmit.", 
                "ATA" : "xxxx", 
                "basis for deferreal" : "", 
                "name" : "Elle Snider", 
                "licence_num" : "1213123r4", 
                "ca" : "lorem ipsun", 
                "oil" : {
                    "L" : "n/a", 
                    "R" : "n/a", 
                    "APU" : "n/a"
                }
            }, 
            {
                "id" : NumberInt(3), 
                "stn" : "MMMJ", 
                "crew/mma" : "MAA", 
                "rep_by" : "Phillip Wood", 
                "discrepancie" : "COM 2 does not transmit.", 
                "ATA" : "xxxx", 
                "basis for deferreal" : "", 
                "name" : "Elle Snider", 
                "licence_num" : "1213123r4", 
                "ca" : "lorem ipsun", 
                "oil" : {
                    "L" : "n/a", 
                    "R" : "n/a", 
                    "APU" : "n/a"
                }
            }
        ]
    }
]}

我正在尝试获取此特定的json结果:具有特定的logbook_number(我的收藏夹中有成千上万个),并且rcca id获取该对象,并具有logbook_number:242146和rcca.id:3,获得与此类似的内容:

{ 
"_id" : ObjectId("51df07b094c6acd67e492f42"), 
"maint_logbooks" : [
    {
        "_id" : NumberInt(2), 
        "logbook_number" : NumberInt(242146), 
        "date" : "2020-04-14T04:00:00Z", 
        "flight_log" : "xxxxx", 
        "flight_num" : "121212", 
        "rcca" : [
            {
                "id" : NumberInt(3), 
                "stn" : "MMMJ", 
                "crew/mma" : "MAA", 
                "rep_by" : "Phillip Wood", 
                "discrepancie" : "COM 2 does not transmit.", 
                "ATA" : "xxxx", 
                "basis for deferreal" : "", 
                "name" : "Elle Snider", 
                "licence_num" : "1213123r4", 
                "ca" : "lorem ipsun", 
                "oil" : {
                    "L" : "n/a", 
                    "R" : "n/a", 
                    "APU" : "n/a"
                }
            }
        ]
    }
]}

我正在尝试使用此查询,但无法解决。

db.getCollection("core_db").find({
  "maint_logbooks": {
    $elemMatch: {
      "rcca": {
        $elemMatch: { "id":3 }
      }
    }
  }
}, { 
  "maint_logbooks": { 
    $elemMatch: {
      "logbook_number": NumberInt(242146)
    }
  }
})

最后,最终我将需要更新“差异”之类的字段,在这一部分中,我没有解决的想法

0 个答案:

没有答案