如何在分组条件下合并文档并在mongo集合中创建新字段?

时间:2019-01-08 04:15:55

标签: mongodb robo3t

这是mongo db集合中的数据迁移。

这是mongo db集合中的数据迁移。 在下面的代码中,我有一些文档。 在通用字段1的基础上,应将所有文档分组并合并为一个文档。在新的合并文档中,我想要一个新的field4,它实际上是该组中的field2和field3对象的列表。并且field2和field3字段不应在单个合并文档中。

这样,在唯一字段1的基础上,我的文档数将在集合中减少。

文档1 {     “ field1”:“ e8b0f2b5-de4a-4449-b2aa-15f009ac4978”,     “ field2”:“ 9911”,     “ field3”:“ a” }

文档2 {     “ field1”:“ e8b0f2b5-de4a-4449-b2aa-15f009ac4978”,     “ field2”:“ 1100”,     “ field3”:“ b” }

文档3 {     “ field1”:“ e8b0f2b5-de4a-4449-b2aa-15f009ac4978”,     “ field2”:“ 0099”,     “ field3”:“ c” }

预期结果

应为1个文档,格式为

{
   “ field1”:“ e8b0f2b5-de4a-4449-b2aa-15f009ac4978”,    “ field4”:[
      {
         “ field2”:“ 9911”,          “ field3”:“一个”       },       {
         “ field2”:“ 1100”,          “ field3”:“ b”       },       {
         “ field2”:“ 0099”,          “ field3”:“ c”       }    ] }

1 个答案:

答案 0 :(得分:0)

您可以将聚合管道与$group$push一起使用

聚合管道

db.t41.aggregate([
    {$group : {
        _id : "$field1", 
        field4 : {$push : {field2 : "$field2", field3 : "$field3"}}
    }},
    {$project : {_id : 0, field1 : "$_id", field4 : "$field4"}}
]).pretty()

收藏

> db.t41.find()
{ "_id" : ObjectId("5c34260a4ffb628e45e69c00"), "field1" : "e8b0f2b5-de4a-4449-b2aa-15f009ac4978", "field2" : "9911", "field3" : "a" }
{ "_id" : ObjectId("5c34260a4ffb628e45e69c01"), "field1" : "e8b0f2b5-de4a-4449-b2aa-15f009ac4978", "field2" : "1100", "field3" : "b" }
{ "_id" : ObjectId("5c34260a4ffb628e45e69c02"), "field1" : "e8b0f2b5-de4a-4449-b2aa-15f009ac4978", "field2" : "0099", "field3" : "c" }
>

结果

> db.t41.aggregate([{$group : {_id : "$field1", field4 : {$push : {field2 : "$field2", field3 : "$field3"}}}}, {$project : {_id : 0, field1 : "$_id", field4 : "$field4"}}]).pretty()
{
        "field1" : "e8b0f2b5-de4a-4449-b2aa-15f009ac4978",
        "field4" : [
                {
                        "field2" : "9911",
                        "field3" : "a"
                },
                {
                        "field2" : "1100",
                        "field3" : "b"
                },
                {
                        "field2" : "0099",
                        "field3" : "c"
                }
        ]
}
>

EDIT-1

这无需对密钥进行硬编码即可

db.t41.aggregate([
    {$group : { _id : "$field1", data : {$push : {$objectToArray : "$$ROOT"}}}}, 
    {$addFields : {data : {$map : {input : "$data", as : "d", in : {$arrayToObject : {$slice : ["$$d", 2, {$size : "$$d"}]}}}}}}
]).pretty()

聚合管道

> db.t41.aggregate([{$group : { _id : "$field1", data : {$push : {$objectToArray : "$$ROOT"}}}}, {$addFields : {data : {$map : {input : "$data", as : "d", in : {$arrayToObject : {$slice : ["$$d", 2, {$size : "$$d"}]}}}}}}]).pretty()
{
        "_id" : "e8b0f2b5-de4a-4449-b2aa-15f009ac4978",
        "data" : [
                {
                        "field2" : "9911",
                        "field3" : "a"
                },
                {
                        "field2" : "1100",
                        "field3" : "b"
                },
                {
                        "field2" : "0099",
                        "field3" : "c"
                }
        ]
}
>