返回特定的mongodb嵌入文档

时间:2014-04-16 09:52:12

标签: javascript mongodb mongodb-query

我想查询mongodb文档以返回特定文档

假设我有以下文档,我只想搜索给定房间的具体细节。说,我想查询返回room_num = 210的注释。我该怎么做?

我能够通过db.locations.findOne查询({" facility.room.room_num":room_num}); 我不知道如何只投影房间。 概括问题,我如何仅投影嵌入的文档特定条目。

我的文档看起来像这样--->

{
  _id: Object("xxx"),
  name: "Campus",
  facilities: [
     { name : "Science",
     rooms : [
          {
             room_num: 210,
             notes: "Chemistry Lab 1",
             Dimension: "x*x"
          },{
             room_num: 120,
             notes: "Chemistry Lab 2",
             Dimension: "x*x"
          }
      ]

    },
    { name : "Arts",
     rooms: [
          {
             room_num: 90,
             notes: "Drawing 1",
             Dimension: "x*x"
          },{
             room_num: 100,
             notes: "Drawing 2",
             Dimension: "x*x"
          }
      ]

    }
  ]

3 个答案:

答案 0 :(得分:0)

尝试以下查询

db.locations.aggregate( 
 { $unwind: "facilities.rooms" },
 { $match: { "facilities.rooms.room_num" : room_num}},
 { $project: { "facilities.rooms.notes" : 1 }}
)

答案 1 :(得分:0)

您可以使用aggregation framework。我相信$project$unwind运营商正是您所寻找的。

db.rooms.aggregate([{
  $project: {
    _id: 0,
    facilities: "$facilities.rooms.room_num"
  }
}, {
  $unwind: "$facilities"
}, {
  $unwind: "$facilities"
}])

你会得到类似的东西:

{
  "result" : [
    {
      "facilities" : 210
    },
    {
      "facilities" : 120
    },
    {
      "facilities" : 90
    },
    {
      "facilities" : 100
    }
  ],
  "ok" : 1
}

答案 2 :(得分:0)

由于你有一个嵌入式阵列(一个在另一个内),你可以用聚合来做到这一点。你只需要做一个" double"展开操作:

db.collection.aggregate([
    { "$unwind": "$facilities" },
    { "$unwind": "$facilities.rooms" },
    { "$match": { "facilities.rooms.room_num": 210 } }
])

如果您真的希望它看起来像原始文档,那么请更进一步:

db.collection.aggregate([
    { "$unwind": "$facilities" },
    { "$unwind": "$facilities.rooms" },
    { "$match": { "facilities.rooms.room_num": 210 } },
    { "$group": {
        "_id": "$_id",
        "name": { "$first": "$name" },
        "facName": { "$first": "$facilities.name" },
        "rooms": { "$push": "$facilities.rooms" }
    }},
    { "$group": {
        "_id": "$_id",
        "name": { "$first": "$name" },
        "facities": { "$push": {
            "name": "$facName" ,
            "rooms": "$rooms"
        }}
    }}
])

因此,根据您的数据,这将是结果:

{
    "_id" : {
            "0" : "x",
            "1" : "x",
            "2" : "x"
    },
    "name" : "Campus",
    "facities" : [
       {
           "name" : "Science",
           "rooms" : [
               {
                   "room_num" : 210,
                   "notes" : "Chemistry Lab 1",
                   "Dimension" : "x*x"
               }
           ]
       }
    ]
}

或者最后如果您需要的只是数组的内部空间部分,那么最后只需使用 $project 语句,如第一个示例所示:

db.campus.aggregate([
    { "$unwind": "$facilities" },
    { "$unwind": "$facilities.rooms" },
    { "$match": { "facilities.rooms.room_num": 210 } },
    { "$project": {
        "_id": 0,
        "room_num": "$facilities.rooms.room_num",
        "notes": "$facilities.rooms.notes",
        "Dimension": "$facilities.rooms.Dimension"
    }}
])

结果如下:

{ "room_num" : 210, "notes" : "Chemistry Lab 1", "Dimension" : "x*x" }

这应该清除它。