1

I have following document structure:

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

I would like to get a flattened version of all room ids along with their parent ids:

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

That would be starting from the room id, going one level up and printing the room id and the building Id. After that again starting from the room id but this time going 2 levels up and printing the room id together with the client id.

Is there a way of achieving this?

With what I have so far I'm not getting the desired results:

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

1 Answers1

0

You can try below aggregations

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" }}
])

Or this

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" }}
])

Both will give same output

[
  {
    "buildingId": "campus1",
    "roomId": 2001
  },
  {
    "buildingId": "campus1",
    "roomId": 2002
  },
  {
    "clientId": 1001,
    "roomId": 2001
  },
  {
    "clientId": 1001,
    "roomId": 2002
  }
]
Ashh
  • 44,693
  • 14
  • 105
  • 132
  • Yes, sir! Many thanks. Could you help me out to "translate" this into java spring? I would really appreciate it. – Sebastian R Jun 22 '18 at 14:31
  • But you haven't mentioned in the question... And sorry I don't know about java spring... But can help you if you put some example of java spring mongodb query – Ashh Jun 22 '18 at 14:39