MongoDB:如何使用$group聚合来获取使用相似字段的所有数据?

Arc*_*rma 0 mongodb mgo

我正在尝试使用 golang mgo 执行查询,以有效地从连接中获取相似的值。
我的结构是这样的:

 result: [
    {
        "_id" : 1,
        "booking_id" : 96,
        "provider_id" : 20,
        "time" : NumberLong(1541158790),
        "arrival_time" : NumberLong(1541158863)
    },
    {
        "_id" : 3,
        "booking_id" : 96,
        "provider_id" : 20,
        "time" : NumberLong(1541158908),
    },
    {
        "_id" : 4,
        "booking_id" : 95,
        "provider_id" : 20,
        "type" : "abc",
        "time" : NumberLong(1541163544),
        "location" : {
            "lat" : 30.711858,
            "lng" : 76.729649
        },
    },
    {
        "_id" : 8,
        "booking_id" : 95,
        "provider_id" : 20,
        "type" : "aaa",
    }
] 
Run Code Online (Sandbox Code Playgroud)

我必须对相似的 booking_id 的数据进行分组,现在如何使用 $group 聚合获取相似的 booking id 的数据。我想要以下结构的数据::

result: [
    0:[
        {
            "_id" : 1,
            "booking_id" : 96,
            "provider_id" : 20,
            "time" : NumberLong(1541158790),
            "arrival_time" : NumberLong(1541158863)
        },
        {
            "_id" : 3,
            "booking_id" : 96,
            "provider_id" : 20,
            "time" : NumberLong(1541158908),
        },
    ],
    1:[
        {
            "_id" : 4,
            "booking_id" : 95,
            "provider_id" : 20,
            "type" : "abc",
            "time" : NumberLong(1541163544),
            "location" : {
                "lat" : 30.711858,
                "lng" : 76.729649
            },
        },
        {
            "_id" : 8,
            "booking_id" : 95,
            "provider_id" : 20,
            "type" : "aaa",
        }
    ]
] 
Run Code Online (Sandbox Code Playgroud)

我创建了一个函数,它返回该集合的结果并使用 $group ,如下所示:

query := []bson.M{
        {"$group": bson.M{
            "_id": bson.M{"booking_id": "$booking_id"},
            "count": bson.M{"$sum": 1}}}}
pipe := getCollection.Pipe(query)
err = pipe.All(&result)
Run Code Online (Sandbox Code Playgroud)

但它会将这个输出返回给我:

[
    {
        "id": 0,
        "booking_id": 0,
        "provider_id": 0
    }
]
Run Code Online (Sandbox Code Playgroud)

这里我只提到两个预订ID数据,我的数据库中有1000条预订ID记录。我想显示按预订 ID 分组的数据,是否可以使用 mongodb $group 聚合?或者如果不是,那么我如何使用 golang 的 mgo 包在 mongodb 中实现这一目标。

And*_*ano 6

您可以使用 $group 和$$ROOT,它引用当前正在管道中处理的文档。您的聚合将类似于:

{ 
    $group: {
      _id: '$booking_id',
      items: {
        $push: '$$ROOT'
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

这会导致这样的结果:

 [
    {
        "_id": 95,
        "items": [
           {
            "_id" : 1,
            "booking_id" : 96,
            "provider_id" : 20,
            "time" : NumberLong(1541158790),
            "arrival_time" : NumberLong(1541158863)
          },
          {
            "_id" : 3,
            "booking_id" : 96,
            "provider_id" : 20,
            "time" : NumberLong(1541158908),
          },
        ]
   }
],
...
Run Code Online (Sandbox Code Playgroud)

编辑:

如果您想同时显示booking_idprovider_id,您可以按这两个字段进行分组并按照您想要的方式投影数据。像这样:

[
{
    $group: {
        _id: { 'booking_id': '$booking_id', 'provider_id': '$provider_id' },
        items: { $push: '$$ROOT' }
    }
},
{
    $project: {
        _id: 0,
        booking_id: '$_id.booking_id',
        provider_id: '$_id.provider_id',
        items: 1 
    }
}
]
Run Code Online (Sandbox Code Playgroud)

给出这个结构:

[
{
    "booking_id": 96,
    "provider_id": 20,
    "items": [
       {
        "_id" : 1,
        "booking_id" : 96,
        "provider_id" : 20,
        "time" : NumberLong(1541158790),
        "arrival_time" : NumberLong(1541158863)
      },
      {
        "_id" : 3,
        "booking_id" : 96,
        "provider_id" : 20,
        "time" : NumberLong(1541158908),
      },
    ]
 }
],
...
Run Code Online (Sandbox Code Playgroud)