glowwormX
glowwormX

Reputation: 123

How to merge mongo inline documents

[{
        "field1" : "1",
        "field2" : [ 
            {
                "f1" : "a",
                "f2" : "b"
            }, 
            {
                "f1" : "aa",
                "f2" : "bb"
            }
        ]
},

{
        "field1" : "2",
        "field2" : [ 
            {
                "f1" : "c",
                "f2" : "d"
            }, 
            {
                "f1" : "cc",
                "f2" : "dd"
            }
        ]
    }]

I want to find out the fields 2 and merge them into the document to the following format:

    {
        "f1" : "a",
        "f2" : "b"
    }, 
    {
        "f1" : "aa",
        "f2" : "bb"
    },
    {
        "f1" : "c",
        "f2" : "d"
    }, 
    {
        "f1" : "cc",
        "f2" : "dd"
    }

Upvotes: 1

Views: 101

Answers (4)

Ashwanth Madhav
Ashwanth Madhav

Reputation: 1134

I think there is no need to group. Just unwind and replaceRoot

db.collection.aggregate([
{"$unwind": "$field2"},
{"$replaceRoot": { "newRoot": "$field2" }}
])

Upvotes: 0

Kevin Smith
Kevin Smith

Reputation: 14436

You can try using a $unwind and a $project within an aggregation query:

db.test.insertMany([
    { "field1" : "1", "field2" : [ { "f1" : "a", "f2" : "b" }, { "f1" : "aa", "f2" : "bb" } ] },
    { "field1" : "2", "field2" : [ { "f1" : "c", "f2" : "d" }, { "f1" : "cc", "f2" : "dd" } ] }
]);

db.test.aggregate([
    {$unwind: "$field2"},
    {$project: {
        "f1": "$field2.f1",
        "f2": "$field2.f2"
    }}
]);

This will give you the following output:

{ "_id" : ObjectId("5c8b70474c52159bf9357567"), "f1" : "a", "f2" : "b" }
{ "_id" : ObjectId("5c8b70474c52159bf9357567"), "f1" : "aa", "f2" : "bb" }
{ "_id" : ObjectId("5c8b70474c52159bf9357568"), "f1" : "c", "f2" : "d" }
{ "_id" : ObjectId("5c8b70474c52159bf9357568"), "f1" : "cc", "f2" : "dd" }

https://play.db-ai.co/m/XItxEAgjhgAB8iV6

Upvotes: 0

deepchudasama
deepchudasama

Reputation: 578

let fakeArray = [
                    {
                        "field1" : "1",
                        "field2" : [ 
                            {
                                "f1" : "a",
                                "f2" : "b"
                            }, 
                            {
                                "f1" : "aa",
                                "f2" : "bb"
                            }
                        ]
                    },
                    {
                        "field1" : "2",
                        "field2" : [ 
                            {
                                "f1" : "c",
                                "f2" : "d"
                            }, 
                            {
                                "f1" : "cc",
                                "f2" : "dd"
                            }
                        ]
                    }
                ];

                let arr = fakeArray.map(el => {
                    return el.field2;
                });

                console.log(arr.flat(1));

you can easily use js features like map and flat

Upvotes: 0

Milan Rakos
Milan Rakos

Reputation: 1763

For input data:

[
{
    "field1" : "1",
    "field2" : [ 
        {
            "f1" : "a",
            "f2" : "b"
        }, 
        {
            "f1" : "aa",
            "f2" : "bb"
        }
    ]
}
,
{
    "field1" : "2",
    "field2" : [ 
        {
            "f1" : "c",
            "f2" : "d"
        }, 
        {
            "f1" : "cc",
            "f2" : "dd"
        }
    ]
}
]

use aggregation:

[
  {
    "$unwind": "$field2"
  },
  {
    "$group": {
      "_id": "$field2"
    }
  },
   {
     "$replaceRoot": { "newRoot": "$_id" }
   }
]

to produce:

[
  {
    "f1": "c",
    "f2": "d"
  },
  {
    "f1": "cc",
    "f2": "dd"
  },
  {
    "f1": "aa",
    "f2": "bb"
  },
  {
    "f1": "a",
    "f2": "b"
  }
]

You can play with this on mongoDB playground: here

Upvotes: 1

Related Questions