Keir
Keir

Reputation: 365

Mongodb subdocument fields union

I need to join two collection from mongodb. First of all I have an aggregation like this:

db.messages.aggregate([
{
 $lookup: {
        from :"channels",
        localField: "channels",
        foreignField: "_id",
        as: "merged_channels"
 }
}
]).pretty()

after this aggragation my documents looks like this:

{
    "_id" : "21ca6117-1f14-4613-9407-db7f3a011142",
    "author" : "03072fad-a8fd-53f3-b25f-abbfaf15b055",
    "title" : "test2",
    "body" : "test2",
    "channels" : [
        "8008d5a8-eb3b-4e55-98c5-a60fd7275bd2",
        "8008d5a8-eb3b-4e55-98c5-a60fd7275bd3"
    ],
    "comments" : [
        {
            "author" : "03072fad-a8fd-53f3-b25f-abbfaf15b055",
            "body" : "comment1",
            "created_at" : ISODate("2018-03-15T07:08:10.018Z")
        },
        {
            "author" : "03072fad-a8fd-53f3-b25f-abbfaf15b055",
            "body" : "testbody",
            "created_at" : ISODate("2018-03-15T07:08:09.366Z")
        }
    ],
    "created_at" : ISODate("2018-03-15T07:08:09.018Z"),
    "updated_at" : ISODate("2018-03-15T07:08:09.366Z"),
    "deleted_at" : null,
    "merged_channels" : [
        {
            "_id" : "8008d5a8-eb3b-4e55-98c5-a60fd7275bd2",
            "author" : "03072fad-a8fd-53f3-b25f-abbfaf15b055",
            "name" : "chan2",
            "members" : [
                {
                    "id" : "8008d5a8-eb3b-4e55-98c5-a60fd7275cb5",
                    "type" : "group"
                }
            ],
            "created_at" : ISODate("2018-03-15T07:08:08.872Z"),
            "updated_at" : ISODate("2018-03-15T07:08:08.872Z"),
            "deleted_at" : null
        },
        {
            "_id" : "8008d5a8-eb3b-4e55-98c5-a60fd7275bd3",
            "author" : "8008d5a8-eb3b-4e55-98c5-a60fd7275fg4",
            "name" : "chan3",
            "members" : [
                {
                    "id" : "8008d5a8-eb3b-4e55-98c5-a60fd7275cb5",
                    "type" : "group"
                },
                {
                    "id" : "8008d5a8-eb3b-4e55-98c5-a60fd7275cb6",
                    "type" : "user"
                },
                {
                    "id" : "8008d5a8-eb3b-4e55-98c5-a60fd7275cb7",
                    "type" : "role"
                }
            ],
            "created_at" : ISODate("2018-03-15T07:08:08.872Z"),
            "updated_at" : ISODate("2018-03-15T07:08:09.358Z"),
            "deleted_at" : null
        }
    ]
}

I want to take members fields from chan2 and chan3, union them and put into the root (message) document. And then remove merged_channels field. Remove the merged field is not a problem, but have no idea how to extract fields from subobject and merge them.

How would I achieve this?

Upvotes: 0

Views: 254

Answers (1)

s7vr
s7vr

Reputation: 75934

You can use below aggregation in 3.4.

$reduce to $concatArrays and project with exclusion to drop the merged_channels field.

db.col.aggregate({
  "$addFields":{
    "merged_arrays":{
      "$reduce":{
        "input":"$merged_channels",
        "initialValue":[],
        "in":{"$concatArrays":["$$value", "$$this.members"]}
      }
    }
  },
  {"$project":{"merged_channels":0}}
})

Upvotes: 1

Related Questions