合并字段相同的集合中的对象

时间:2020-02-10 16:49:41

标签: mongodb mongodb-query aggregation-framework

我正在尝试合并和更新4个字段(名称,面板,节,小节)相同的对象。

合并的数据1:名称=“现场手册v1.1”,区域=“客户”,子区域=“手册”,面板=“革命系列(R3系列)”

合并的数据2:名称=“现场手册v1.1”,区域=“客户”,子区域=“手册”,面板=“生态安全摘要器(E3系列)”

初始对象

{
    "_id" : ObjectId("5d35e1fd02819f105326c84e"),
    "deleted" : false,
    "files" : [
        {
            "ext" : "pdf",
            "file" : "Revolution Series Digester Field Handbook R3 Series v1.1 150 DPI.pdf"
        }
    ],
    "name" : "Field Handbook v1.1",
    "section" : "customer",
    "subsection" : "manuals",
    "tags" : [
        "customer",
        "manuals"
    ],
    "panel" : "Revolution Series (R3 Series)"
}


{
    "_id" : ObjectId("5d35e1fd02819f105326c851"),
    "deleted" : false,
    "files" : [
        {
            "ext" : "link",
            "file" : "http://docs.biohitech.com/r3/handbook"
        }
    ],
    "name" : "Field Handbook v1.1",
    "section" : "customer",
    "subsection" : "manuals",
    "tags" : [
        "customer",
        "manuals"
    ],
    "panel" : "Revolution Series (R3 Series)"
}


{
    "_id" : ObjectId("5d35e1fd02819f105326c856"),
    "deleted" : false,
    "files" : [
        {
            "ext" : "pdf",
            "file" : "Eco-Safe Digester Field Handbook E3 Series v1.1 150 DPI.pdf"
        }
    ],
    "name" : "Field Handbook v1.1",
    "section" : "customer",
    "subsection" : "manuals",
    "tags" : [
        "customer",
        "manuals"
    ],
    "panel" : "Eco-Safe Digester (E3 Series)"
}


{
    "_id" : ObjectId("5d35e1fd02819f105326c857"),
    "deleted" : false,
    "files" : [
        {
            "ext" : "link",
            "file" : "http://docs.biohitech.com/e3/handbook/"
        }
    ],
    "name" : "Field Handbook v1.1",
    "section" : "customer",
    "subsection" : "manuals",
    "tags" : [
        "customer",
        "manuals"
    ],
    "panel" : "Eco-Safe Digester (E3 Series)"
}

预期结果

{
    "_id" : ObjectId("5d35e1fd02819f105326c84e"),
    "deleted" : false,
    "files" : [
        {
            "ext" : "pdf",
            "file" : "Revolution Series Digester Field Handbook R3 Series v1.1 150 DPI.pdf"
        },
        {
            "ext" : "link",
            "file" : "http://docs.biohitech.com/r3/handbook"
        }
    ],
    "name" : "Field Handbook v1.1",
    "section" : "customer",
    "subsection" : "manuals",
    "tags" : [
        "customer",
        "manuals"
    ],
    "panel" : "Revolution Series (R3 Series)"
}


{
    "_id" : ObjectId("5d35e1fd02819f105326c856"),
    "deleted" : false,
    "files" : [
        {
            "ext" : "pdf",
            "file" : "Eco-Safe Digester Field Handbook E3 Series v1.1 150 DPI.pdf"
        },
        {
            "ext" : "link",
            "file" : "http://docs.biohitech.com/e3/handbook/"
        }
    ],
    "name" : "Field Handbook v1.1",
    "section" : "customer",
    "subsection" : "manuals",
    "tags" : [
        "customer",
        "manuals"
    ],
    "panel" : "Eco-Safe Digester (E3 Series)"
}

1 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

 db.collection.aggregate([
    {
        $unwind: {
            path: "$files",
            preserveNullAndEmptyArrays: true
        }
    },
    {
        $group: {
            _id: {
                "name": "$name",
                "section": "$section",
                "subsection": "$subsection",
                "panel": "$panel"
            },
            data: {
                $first: "$$ROOT"
            },
            files: {
                $push: "$files"
            }
        }
    },
    {
        $addFields: {
            "data.files": "$files"
        }
    },
    {
        $replaceRoot: {
            newRoot: "$data"
        }
    },
    /** writes to new collection named 'collection_new' will override collection if that name already exists */
    { $out: "collection_new" }
])

测试: MongoDB-Playground

一旦您认为collection_new中的数据很好,那么您可能需要删除现有集合并将其重命名为旧名称/实际名称,还要检查索引。如果可以升级到>4.2,则可以使用$merge,它将文档附加到现有集合中,使用起来更加简洁。