【问题标题】:How can I get one field of subdocument and all _id from its parents accross all levels?如何从所有级别的父级获取子文档的一个字段和所有 _id?
【发布时间】:2018-06-22 13:49:23
【问题描述】:

我有以下文档结构:

{
    clientId: 1001
    buildings: [
        _id: campus1
        rooms: [
            {
                _id: 2001
                name: conference
            },
            {
                _id: 2002
                name: meeting
            },
        ]
    ]
}

我想获得所有房间 ID 及其父 ID 的扁平化版本:

{
    roomId: 2001
    buildingId: campus1
},
{
    roomId: 2001
    clientId: 1001
},
{
    roomId: 2002
    buildingId: campus1
},
{
    roomId: 2002
    clientId: 1001
}

这将从房间 ID 开始,向上一层打印房间 ID 和建筑物 ID。之后再次从房间 id 开始,但这次向上 2 级并打印房间 id 和客户 id。

有没有办法做到这一点?

到目前为止,我没有得到想要的结果:

db.collection.aggregate(
[
      { $unwind:"$buildings" },
      { $unwind:"$building.rooms" },
    {
        $project: {
            _id: 0,
            client: "$_id",
            building: "$buildings._id",
            room: "$buildings.floors.rooms._id",
        }
    },
    { $unwind:"$room" }
]
)

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework spring-data-mongodb


    【解决方案1】:

    您可以尝试以下聚合

    db.collection.aggregate([
      { "$unwind": "$buildings" },
      { "$unwind": "$buildings.rooms" },
      { "$group": {
        "_id": null,
        "firstArray": {
          "$push": {
            "roomId": "$buildings.rooms._id",
            "buildingId": "$buildings._id"
          }
        },
        "secondArray": {
          "$push": {
            "roomId": "$buildings.rooms._id",
            "clientId": "$clientId"
          }
        }
      }},
      { "$project": {
        "data": {
          "$concatArrays": ["$firstArray", "$secondArray"]
        }
      }},
      { "$unwind": "$data" },
      { "$replaceRoot": { "newRoot": "$data" }}
    ])
    

    或者这个

    db.collection.aggregate([
      { "$facet": {
        "firstArray": [
          { "$unwind": "$buildings" },
          { "$unwind": "$buildings.rooms" },
          { "$project": {
            "roomId": "$buildings.rooms._id",
            "buildingId": "$buildings._id",
            "_id": 0
          }}
        ],
        "secondArray": [
          { "$unwind": "$buildings" },
          { "$unwind": "$buildings.rooms" },
          { "$project": {
            "roomId": "$buildings.rooms._id",
            "clientId": "$clientId",
            "_id": 0
          }}
        ]
      }},
      { "$project": {
        "data": {
          "$concatArrays": ["$firstArray", "$secondArray"]
        }
      }},
      { "$unwind": "$data" },
      { "$replaceRoot": { "newRoot": "$data" }}
    ])
    

    两者都会给出相同的输出

    [
      {
        "buildingId": "campus1",
        "roomId": 2001
      },
      {
        "buildingId": "campus1",
        "roomId": 2002
      },
      {
        "clientId": 1001,
        "roomId": 2001
      },
      {
        "clientId": 1001,
        "roomId": 2002
      }
    ]
    

    【讨论】:

    • 是的,先生!非常感谢。你能帮我把它“翻译”成java spring吗?我真的很感激。
    • 但你没有在问题中提到......对不起,我不知道java spring......但是如果你放一些java spring mongodb查询的例子可以帮助你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2013-07-07
    • 2017-12-08
    相关资源
    最近更新 更多