如何在golang中进行Mongodb聚合

I have a MongoDB collection like this:

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

I want to distinct it by "device_id" AND sort it by "pubtime".

I know that Golang could use pipe to do it. But I don't know how to do it. What I tried:

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)

The result is empty.

It is ok in MongoDB:

db.collections.aggregate({"$group":
                        {"_id":{"device_id":"$device_id"},
                        "pubtime":{"$last": "$pubtime"} ,
                       "value":{"$last": "$value"} ,
                        }});
doumei8126
doumei8126 <pre>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}}管道:=c.Pipe([]bson.M{o3})var结果[]bson.M{}_=pipe.All(&result)fmt.Println(result)</pre>它为空。
接近 3 年之前 回复

2个回答

You're not checking the errors, that's your main problem. Pipe.All() returns an error which you gracefully discard. Don't do that.

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

This will print:

[] a group's fields must be specified in an object

The error says it all. The value of $group must be e.g. a bson.M value, and not a slice of 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)

Now the output will be:

[map[_id:map[device_id:12222] pubtime:2017-11-07 10:41:49.452 +0100 CET] map[_id:map[device_id:1111] pubtime:2017-11-07 10:40:11.204 +0100 CET] map[_id:map[device_id:1232213] pubtime:2017-11-07 10:42:13.658 +0100 CET]] <nil>

So it works.

And to make the results sorted by pubtime, use $sort. Here's the final code:

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)

If you want the results sorted in descending order, then use:

{"$sort": bson.M{"pubtime": -1}}

Also note that when grouping, if the group _id is a single field, you don't need to wrap it into an object, you can simply use $device_id as the group id:

"$group": bson.M{
    "_id":     "$device_id",
    "pubtime": bson.M{"$last": "$pubtime"},
},
dpxnh80648
dpxnh80648 非常感谢!非常感谢!非常感谢!非常感谢!
接近 3 年之前 回复



可以使用 $ group mongodb聚合管道中的$ first </ p>

Mongodb shell查询</ p>

  db.collection.aggregate([
{$ group :{_id:{device_id:“ $ device_id”},发布时间:{$ first:“ $ pubtime”}}}
]);
</ code> </ pre>

结果 在mongo shell中执行上述查询后得到的结果</ p>

  {“ _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”)}
</ code> </ pre>
</ div>

展开原文

原文

It can be done using $group and $first in mongodb aggregation pipeline

Mongodb shell query

db.collection.aggregate([
    {$group: {_id:{device_id:"$device_id"}, pubtime:{$first:"$pubtime"}}}
]);

The result which we get after executing the above query in 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") }

dpfad62426
dpfad62426 我的Mongodb外壳还可以。
接近 3 年之前 回复
doueta6642
doueta6642 抱歉,我想知道在Golang的做法。
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐