简体   繁体   中英

Aggregation using two sub object arrays MongoDB

Got two arrays in different collections object. I want to get all fields from two arrays in one object specifying and uuid that is common.

This are the two arrays from two different objects:

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

{
    "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. I have multiple $list, so on unwind there must be two objects with the same uuid, if not result must be null.

The following query can get us the expected 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:

{
    "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 . Each group holds the set of all list elements with the same 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()`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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