[使用两个子对象数组MongoDB进行聚合

问题描述 投票:0回答:2

有两个不同集合对象中的数组。我想从两个数组中的所有字段中指定一个对象,并指定通用的uuid。

这是来自两个不同对象的两个数组:

{
    "_id" : ObjectId("5a1ea4a4a1a13eaecf571267"),
    "storage" : "events",
    "list" : [
        {
            "uuid" : "5a03c1e0e31a11e7b8c2e398bcd9f882",
            "type" : "TYPE",
            "desc" : "DESC",
            "when" : 1513934100000,
            "loc" : "LOC",
            "schedule" : [ ]
        },
        {
            "uuid" : "1b45a340e70911e7b8c2e398bcd9f882",
            "type" : "TYPE1",
            "desc" : "DESC1",
            "when" : 1514624400000,
            "loc" : "LOC1",
            "schedule" : []
        }
        }
}
{
    "_id" : ObjectId("5a297001840b2aba87a5b1eb"),
    "storage" : "control",
    "list" : [
        {
            "uuid" : "5a03c1e0e31a11e7b8c2e398bcd9f882",
            "missing" : [
                {
                    "user" : "user",
                    "instrument" : "inst"
                },
                {
                    "user" : "user",
                    "instrument" : "inst"
                }
            ],
            "when" : 1513934100000
        },
                {
            "uuid" : "1b45a340e70911e7b8c2e398bcd9f882",
            "missing" : [
                {
                    "user" : "user",
                    "instrument" : "inst"
                },
                {
                    "user" : "user",
                    "instrument" : "inst"
                }
            ],
            "when" : 1514624400000
        },
         }
}

所需的结果是:(因为给定的uuid为5a03c1e0e31a11e7b8c2e398bcd9f882)

{
    "list" : [
        {
            "uuid" : "5a03c1e0e31a11e7b8c2e398bcd9f882",
            "type" : "TYPE",
            "desc" : "DESC",
            "when" : 1513934100000,
            "loc" : "LOC",
            "schedule" : [ ],
            "missing" : [
                {
                    "user" : "user",
                    "instrument" : "inst"
                },
                {
                    "user" : "user",
                    "instrument" : "inst"
                }
           ]
        }
  }

事实是,如果没有两个uuid匹配,则结果必须为null。我有多个$ list,所以在展开时必须有两个具有相同uuid的对象,如果没有,结果必须为null。

node.js mongodb aggregation
2个回答
0
投票

以下查询可以为我们提供预期的输出:

db.collection.aggregate([
    {
        $unwind:"$list"
    },
    {  
        $group:{
            "_id":"$list.uuid",
            "list":{
                $push:"$list"
            }
        }
    },
    {
       $project:{
            "merged":{
                $reduce:{
                    "input":"$list",
                    "initialValue":{},
                    "in":{
                        $mergeObjects:["$$this","$$value"]
                    }
                }
            }
        }
    },
    {
        $group:{
            "_id":null,
            "list":{
                $push: "$merged"
            }
        }
    },
    {
        $project:{
            "_id":0,
            "storage" : "result",
            "list":1
        }
    }
]).pretty()

数据集:

{
    "_id" : ObjectId("5a1ea4a4a1a13eaecf571267"),
    "storage" : "events",
    "list" : [
        {
            "uuid" : "5a03c1e0e31a11e7b8c2e398bcd9f882",
            "type" : "TYPE",
            "desc" : "DESC",
            "when" : 1513934100000,
            "loc" : "LOC",
            "schedule" : [ ]
        },
        {
            "uuid" : "1b45a340e70911e7b8c2e398bcd9f882",
            "type" : "TYPE1",
            "desc" : "DESC1",
            "when" : 1514624400000,
            "loc" : "LOC1",
            "schedule" : [ ]
        }
    ]
}
{
    "_id" : ObjectId("5a297001840b2aba87a5b1eb"),
    "storage" : "control",
    "list" : [
        {
            "uuid" : "5a03c1e0e31a11e7b8c2e398bcd9f882",
            "missing" : [
                {
                    "user" : "user",
                    "instrument" : "inst"
                },
                {
                    "user" : "user",
                    "instrument" : "inst"
                }
            ],
            "when" : 1513934100000
        },
        {
            "uuid" : "1b45a340e70911e7b8c2e398bcd9f882",
            "missing" : [
                {
                    "user" : "user",
                    "instrument" : "inst"
                },
                {
                    "user" : "user",
                    "instrument" : "inst"
                }
            ],
            "when" : 1514624400000
        }
    ]
}

输出:

{
    "list" : [
        {
            "uuid" : "1b45a340e70911e7b8c2e398bcd9f882",
            "missing" : [
                {
                    "user" : "user",
                    "instrument" : "inst"
                },
                {
                    "user" : "user",
                    "instrument" : "inst"
                }
            ],
            "when" : 1514624400000,
            "type" : "TYPE1",
            "desc" : "DESC1",
            "loc" : "LOC1",
            "schedule" : [ ]
        },
        {
            "uuid" : "5a03c1e0e31a11e7b8c2e398bcd9f882",
            "missing" : [
                {
                    "user" : "user",
                    "instrument" : "inst"
                },
                {
                    "user" : "user",
                    "instrument" : "inst"
                }
            ],
            "when" : 1513934100000,
            "type" : "TYPE",
            "desc" : "DESC",
            "loc" : "LOC",
            "schedule" : [ ]
        }
    ],
    "storage" : "result"
}

查询分析:我们首先要展开list数组,然后根据list.uuid进行分组。每个组包含具有相同UUID的所有列表元素的集合。稍后,列表元素将合并为一个。


0
投票
`db.a9e34aa77f3474fbab5e9827942fbdd.aggregate([
    {
        $project: {
            'list': {
                $filter: {
                    input: '$list',
                    as: 'item',
                    cond: {
                        $eq: ['$$item.uuid', '15d055a0d5d711e8bd45bff08b1fb980']
                    }
                }
            }
        }
    },
    {
        $unwind:"$list"
    },
    {
        $group:{
            "_id":"$list.uuid",
            "list":{
                $push:"$list"
            }
        }
    },
    {
        $project:{
            "merged":{
                $reduce:{
                    "input":"$list",
                    "initialValue":{},
                    "in":{
                        $mergeObjects:["$$this","$$value"]
                    }
                }
            }
        }
    },
    {
        $group:{
            "_id":null,
            "list":{
                $push: "$merged"
            }
        }
    },
    {
        $project:{
            "_id":0,
            "list":1
        }
    }
]).pretty()`
© www.soinside.com 2019 - 2024. All rights reserved.