如何在golang进行Mongodb聚合

如何在golang进行Mongodb聚合,mongodb,go,pipe,aggregate,mgo,Mongodb,Go,Pipe,Aggregate,Mgo,我有这样一个MongoDB系列: { "_id" : ObjectId("5a017ee061313781045889ea"), "device_id" : "1232213", "value" : "23233", "pubtime" : ISODate("2017-11-07T09:37:37.006Z") } { "_id" : ObjectId("5a017f7b61313781045889eb"), "device_id" : "1111", "value" : "2323

我有这样一个MongoDB系列:

{ "_id" : ObjectId("5a017ee061313781045889ea"),  "device_id" : "1232213",   "value" : "23233", "pubtime" : ISODate("2017-11-07T09:37:37.006Z") }
{ "_id" : ObjectId("5a017f7b61313781045889eb"),  "device_id" : "1111",   "value" : "23233", "pubtime" : ISODate("2017-11-07T09:40:11.204Z") }
{ "_id" : ObjectId("5a017fdd61313781045889ec"),  "device_id" : "12222",   "value" : "23233", "pubtime" : ISODate("2017-11-07T09:41:49.452Z") }
{ "_id" : ObjectId("5a017ff561313781045889ed"),  "device_id" : "1232213",   "value" : "23233", "pubtime" : ISODate("2017-11-07T09:42:13.658Z") }
我想通过
“device\u id”
来区分它,并通过
“pubtime”
对它进行排序

我知道Golang可以用烟斗来做这件事。但我不知道怎么做。我尝试的是:

o1 := bson.M{"_id": bson.M{"device_id": "$device_id"}}

o2 := bson.M{"pubtime": bson.M{"$last": "$pubtime"}}
o3 := bson.M{"$group": []bson.M{o1, o2}}
pipe := c.Pipe([]bson.M{o3})
var result = []bson.M{}
_ = pipe.All(&result)
fmt.Println(result)
结果是空的

在MongoDB中也可以:

db.collections.aggregate({"$group":
                        {"_id":{"device_id":"$device_id"},
                        "pubtime":{"$last": "$pubtime"} ,
                       "value":{"$last": "$value"} ,
                        }});
可以使用和在mongodb聚合管道中完成

Mongodb shell查询

db.collection.aggregate([
    {$group: {_id:{device_id:"$device_id"}, pubtime:{$first:"$pubtime"}}}
]);
在mongo shell中执行上述查询后得到的结果

{ "_id" : { "device_id" : "12222" }, "pubtime" : ISODate("2017-11-07T09:41:49.452Z") }
{ "_id" : { "device_id" : "1111" }, "pubtime" : ISODate("2017-11-07T09:40:11.204Z") }
{ "_id" : { "device_id" : "1232213" }, "pubtime" : ISODate("2017-11-07T09:37:37.006Z") }

你没有检查错误,这是你的主要问题
Pipe.All()
返回一个错误,您可以优雅地放弃该错误。不要那样做

var result = []bson.M{}
err := pipe.All(&result)
fmt.Println(result, err)
这将打印:

[] a group's fields must be specified in an object
错误说明了一切。
$group
的值必须是例如
bson.M
值,而不是
bson.M
的一部分:

o3 := bson.M{"$group": bson.M{
    "_id":     bson.M{"device_id": "$device_id"},
    "pubtime": bson.M{"$last": "$pubtime"},
}}
pipe := c.Pipe([]bson.M{o3})
var result = []bson.M{}
err := pipe.All(&result)
fmt.Println(result, err)
现在输出为:

[map[\u id:map[device\u id:12222]pubtime:2017-11-07 10:41:49.452+0100 CET]map[\u id:map[device\u id:1111]pubtime:2017-11-07 10:40:11.204+0100 CET]map[\u id:map[device\u id:1232213]pubtime:2017-11-07 10:42:13.658+0100 CET]

所以它起作用了

要按
pubtime
对结果进行排序,请使用
$sort
。以下是最终代码:

pipe := c.Pipe([]bson.M{
    {
        "$group": bson.M{
            "_id":     bson.M{"device_id": "$device_id"},
            "pubtime": bson.M{"$last": "$pubtime"},
        },
    },
    {"$sort": bson.M{"pubtime": 1}},
})
var result = []bson.M{}
err := pipe.All(&result)
fmt.Println(result, err)
如果希望结果按降序排序,请使用:

{"$sort": bson.M{"pubtime": -1}}
还请注意,分组时,如果组
\u id
是单个字段,则不需要将其包装到对象中,只需使用
$device\u id
作为组id:

"$group": bson.M{
    "_id":     "$device_id",
    "pubtime": bson.M{"$last": "$pubtime"},
},

o1:=bson.M{“{”id“:bson.M{”device\u id:“$device\u id”}o2:=bson.M{“pubtime”:bson.M{“$last”:“$pubtime”}o3:=bson.M{”$group:[]bson.M{o1,o2}管道:=c.pipe([]bson.M{})var result[]bson.M{o3}var result[]bson.M{o3}管道。All(&result)fmt.printang(result)很抱歉,我想知道如何在我的shell中找到一个空的goldx!太多了!太多了!太多了!