Reputation: 23
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"} ,
}});
Upvotes: 2
Views: 2841
Reputation: 417472
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"},
},
Upvotes: 1
Reputation: 5466
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") }
Upvotes: 0