如何在mongodb中执行分组

时间:2017-09-07 06:49:37

标签: node.js mongodb mongodb-query

我在MongoDB中拥有这些数据,我希望对这些数据进行分组,以便以单一数组格式在相同的ID下获取所有跟踪号。

* 1 */
{
    "_id" : ObjectId("597056182a93692b4c7691bf"),
     "Sid":1,   
    "Carriers" : [ 
        {
            "Tracking" : [ 
                {
                    "TrackingNo" : "121_dom"
                }
            ]
        }, 
        {
            "Tracking" : [ 
                {
                    "TrackingNo" : "779591314278"
                }, 
                {
                    "TrackingNo" : "779591314039"
                }, 
                {
                    "TrackingNo" : "779591314231"
                }
            ]
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("597057338c65c002e4285fb3"),
    "Sid":2,    
    "Carriers" : [ 
        {
            "Tracking" : [ 
                {
                    "TrackingNo" : "122_dom"
                }
            ]
        }, 
        {
            "Tracking" : [ 
                {
                    "TrackingNo" : "77959131427"
                }, 
                {
                    "TrackingNo" : "77959131403"
                }
            ]
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("5980ae7ecc71b581b626d20b"),
     "Sid":3,     
    "Carriers" : [ 
        {
            "Tracking" : [ 
                {
                    "TrackingNo" : "123_dom"
                }
            ]
        }, 
        {
            "Tracking" : [ 
                {
                    "TrackingNo" : "77959131408"
                }, 
                {
                    "TrackingNo" : "779591314059"
                }, 
                {
                    "TrackingNo" : "779591315551"
                }
            ]
        }
    ]
}

即在_id上执行group by操作,它在单个数组中显示相同_Id下的所有跟踪号,表示结果集将如下所示:

{
    "_id" : ObjectId("597056182a93692b4c7691bf"),
     "Sid":1,     
    "Carriers" : [ 
        {
            "Tracking" : [ 
                {
                    "TrackingNo" : "121_dom"
                }
                {
                    "TrackingNo" : "779591314278"
                }, 
                {
                    "TrackingNo" : "779591314039"
                }, 
                {
                    "TrackingNo" : "779591314231"
                }
            ]
        }
    ]
}

2 个答案:

答案 0 :(得分:0)

由于数组中有数组,首先需要展开两次,然后重新分组。以下查询应该有效。您需要根据需要在$ group和$ project中投影其他字段。

db.shipments.aggregate(
  {$unwind:'$Carriers'},
  {$unwind: '$Carriers.Tracking'},
  {$group: {_id:'$_id', c: {$push:  '$Carriers.Tracking'}}},
  {$project:{Carriers:  {Tracking: '$c'}}}
)

答案 1 :(得分:0)

根据上述问题中的描述,请尝试将以下聚合查询执行到MongoDB shell中作为上述问题的解决方案。

db.shipments.aggregate(

// Pipeline
[
    // Stage 1
    {
        $unwind: {
            path: '$Carriers'
        }
    },

    // Stage 2
    {
        $unwind: {
            path: '$Carriers.Tracking'
        }
    },

    // Stage 3
    {
        $group: {
            _id: {
                _id: '$_id',
                Sid: '$Sid'
            },
            Carriers: {
                $addToSet: {
                    TrackingNo: '$Carriers.Tracking.TrackingNo'
                }
            }

        }
    },

    // Stage 4
    {
        $group: {
            _id: '$_id',
            Carriers: {
                $addToSet: {
                    Tracking: '$Carriers'
                }
            }
        }
    },

    // Stage 5
    {
        $project: {
            _id: '$_id._id',
            Sid: '$_id.Sid',
            Carriers: '$Carriers'
        }
    }

]

);