如何合并Mongo内联文档

时间:2019-03-15 08:23:36

标签: mongodb

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

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

我想找出字段2并将它们合并为以下格式的文档:

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

4 个答案:

答案 0 :(得分:1)

对于输入数据:

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

使用聚合:

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

产生:

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

您可以在mongoDB操场上玩这个游戏:here

答案 1 :(得分:0)

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));

您可以轻松使用诸如mapflat之类的js功能

答案 2 :(得分:0)

您可以尝试在聚合查询中使用$unwind$project

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

这将为您提供以下输出:

{ "_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

答案 3 :(得分:0)

我认为没有必要group。只是unwindreplaceRoot

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