在MongoDB集合中的对象数组中检索多个查询元素

时间:2018-04-02 12:57:50

标签: mongodb mongodb-query aggregation-framework

我想找到所有带有“AHU”的区域数据:“C”。首先,我在没有投影的情况下进行查询并得到这些。

> db.buildings.find({"zone.AHU": "C"}).pretty()
{
    "_id" : ObjectId("5aba4460a042dc4a2fdf26cd"),
    "name" : "Test Street",
    "coordinate" : [
        12,
        31
    ],
    "yearlyEnergyCost" : 1444,
    "zone" : [
        {
            "name" : "AHU-C-Z2",
            "_id" : ObjectId("5aba4460a042dc4a2fdf26ce"),
            "AHU" : "C",
            "precooling" : [ ],
            "subZone" : [ ]
        },
        {
            "name" : "AHU-D-Z1",
            "_id" : ObjectId("5abc7528100730697163a3ab"),
            "AHU" : "D",
            "precooling" : [ ],
            "subZone" : [ ]
        },
        {
            "name" : "AHU-C-Z1",
            "AHU" : "C",
            "_id" : ObjectId("5ac09c898249affa03506eff"),
            "precooling" : [ ],
            "subZone" : [ ]
        },
        {
            "name" : "AHU-C-Z3",
            "AHU" : "C",
            "_id" : ObjectId("5ac09c898249affa03506efe"),
            "precooling" : [ ],
            "subZone" : [ ]
        }
    ],
    "__v" : 2
}

但是,当我使用$ elemMatch时,它只返回带有“AHU”的第一个区域元素:“C”

> db.buildings.find({"zone.AHU": "C"}, {_id: 0, zone: {$elemMatch: {AHU: "C"}}}).pretty()
{
    "zone" : [
        {
            "name" : "AHU-C-Z2",
            "_id" : ObjectId("5aba4460a042dc4a2fdf26ce"),
            "AHU" : "C",
            "precooling" : [ ],
            "subZone" : [ ]
        }
    ]
}

doc,我意识到$ elemMatch(投影)只检索第一个,但我怎样才能检索所有相应的(AHU-C-Z1,AHU-C-Z2,AHU-C-Z3) )?谢谢。

这是集合:

{  
   "_id":{  
      "$oid":"5aa65bc96996e045104116e7"
   },
   "name":"Talker Street",
   "coordinate":[  
      11.82,
      -9.26
   ],
   "yearlyEnergyCost":100,
   "zone":[  
      {  
         "name":"AHU-B-Z1",
         "_id":{  
            "$oid":"5aa65bc96996e045104116e8"
         },
         "precooling":[  
            {  
               "_id":{  
                  "$oid":"5aa73a7d2f991a657fd52c7e"
               },
               "resultPrecool":{  
                  "$oid":"5aa73a7d2f991a657fd52b5d"
               },
               "dateRun":{  
                  "$date":"2018-03-14T00:00:00.000+0000"
               },
               "lastUpdated":{  
                  "$date":"2018-03-13T02:41:02.086+0000"
               }
            }
         ]
      },
      {  
         "name":"AHU-B-Z2",
         "_id":{  
            "$oid":"5aa9f1f8131e6412c17d71d3"
         },
         "precooling":[  

         ]
      },
      {  
         "name":"AHU-B-Z3",
         "_id":{  
            "$oid":"5aa9f1f8131e6412c17d71d2"
         },
         "precooling":[  

         ]
      }
   ],
   "__v":19
}{  
   "_id":{  
      "$oid":"5aba4460a042dc4a2fdf26cd"
   },
   "name":"Test Street",
   "coordinate":[  
      12,
      31
   ],
   "yearlyEnergyCost":1444,
   "zone":[  
      {  
         "name":"AHU-C-Z2",
         "_id":{  
            "$oid":"5aba4460a042dc4a2fdf26ce"
         },
         "AHU":"C",
         "precooling":[  

         ],
         "subZone":[  

         ]
      },
      {  
         "name":"AHU-D-Z1",
         "_id":{  
            "$oid":"5abc7528100730697163a3ab"
         },
         "AHU":"D",
         "precooling":[  

         ],
         "subZone":[  

         ]
      },
      {  
         "name":"AHU-C-Z1",
         "AHU":"C",
         "_id":{  
            "$oid":"5ac09c898249affa03506eff"
         },
         "precooling":[  

         ],
         "subZone":[  

         ]
      },
      {  
         "name":"AHU-C-Z3",
         "AHU":"C",
         "_id":{  
            "$oid":"5ac09c898249affa03506efe"
         },
         "precooling":[  

         ],
         "subZone":[  

         ]
      }
   ],
   "__v":2
}{  
   "_id":{  
      "$oid":"5aba46c41c8d5e4b52462aea"
   },
   "name":"123123",
   "coordinate":[  
      12,
      31
   ],
   "yearlyEnergyCost":12321,
   "zone":[  
      {  
         "name":"123423",
         "_id":{  
            "$oid":"5aba46c41c8d5e4b52462aeb"
         },
         "precooling":[  

         ],
         "subZone":[  

         ]
      }
   ],
   "__v":0
}

1 个答案:

答案 0 :(得分:0)

您可以使用$redact运算符:

db.buildings.aggregate([
{$match:{"zone.AHU":{$exists:true}}},
{$redact:{
  $cond:{
       if:{$or:[{$eq:["$AHU","C"]},{$not: "$AHU"}]},
       then:"$$DESCEND",
       else:"$$PRUNE"   
     }  
   }}  
]) 

请记住,{$not: "$AHU"}非常重要,因此不会排除top元素。如果没有添加,将跳过top元素,从而跳过整个嵌入文档。

输出:

{
"_id" : ObjectId("5aba4460a042dc4a2fdf26cd"),
"name" : "Test Street",
"coordinate" : [ 
    12, 
    31
],
"yearlyEnergyCost" : 1444,
"zone" : [ 
    {
        "name" : "AHU-C-Z2",
        "_id" : ObjectId("5aba4460a042dc4a2fdf26ce"),
        "AHU" : "C",
        "precooling" : [],
        "subZone" : []
    }, 
    {
        "name" : "AHU-C-Z1",
        "AHU" : "C",
        "_id" : ObjectId("5ac09c898249affa03506eff"),
        "precooling" : [],
        "subZone" : []
    }, 
    {
        "name" : "AHU-C-Z3",
        "AHU" : "C",
        "_id" : ObjectId("5ac09c898249affa03506efe"),
        "precooling" : [],
        "subZone" : []
    }
],
"__v" : 2
}