简体   繁体   English

聚合使用两个子 object arrays MongoDB

[英]Aggregation using two sub object arrays MongoDB

Got two arrays in different collections object.在不同的 collections object 中有两个 arrays。 I want to get all fields from two arrays in one object specifying and uuid that is common.我想在一个 object 指定和常见的 uuid 中从两个 arrays 获取所有字段。

This are the two arrays from two different objects:这是来自两个不同对象的两个 arrays:

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

Desired result is: (Because given uuid was 5a03c1e0e31a11e7b8c2e398bcd9f882)期望的结果是:(因为给定的 uuid 是 5a03c1e0e31a11e7b8c2e398bcd9f882)

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

The thing is if there isn't two matches of uuid the result must be null.问题是如果没有两个匹配的 uuid,则结果必须是 null。 I have multiple $list, so on unwind there must be two objects with the same uuid, if not result must be null.我有多个 $list,所以展开时必须有两个具有相同 uuid 的对象,如果不是,结果必须是 null。

The following query can get us the expected output:以下查询可以得到我们预期的 output:

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

Data set:数据集:

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

Output: Output:

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

Query analysis: We are first unwinding the list array and then grouping on the basis of list.uuid .查询分析:我们首先展开list数组,然后根据list.uuid进行分组。 Each group holds the set of all list elements with the same UUID.每个组包含具有相同 UUID 的所有列表元素的集合。 Later on, the list elements are merged into one.稍后,列表元素合并为一个。

`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()`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM