问题背景是我想使用golang mgo.v2 liabary从MongoDB数据中检索聚合数据。
我有一个集合数据集,如下所示。集合名称是useragents
Event event = new Event()
{
Summary = "Appointment",
Location = "Somewhere",
Start = new EventDateTime() {
DateTime = new DateTime("2016-10-14T10:00:00")
TimeZone = "Europe/Warsaw"
},
End = new EventDateTime() {
DateTime = new DateTime("2016-10-14T11:00:00")
TimeZone = "Europe/Warsaw"
},
Recurrence = new String[] {
"RRULE:FREQ=YEARLY;BYMONTHDAY=14;BYMONTH=10"
},
Attendees = new List<EventAttendee>()
{
new EventAttendee() { Email: "attendeeEmail" },
// ...
}
};
Event recurringEvent = service.Events.Insert(event, "primary").Fetch();
Console.WriteLine(recurringEvent.Id);
我已经通过堆栈溢出社区的帮助解决了我想要的MongoDB shell脚本。但是当我使用mgo.v2库在golang中实现它时,我感到很麻烦。
这是mongodb shell脚本
{
"_id" : ObjectId("57f940c4932a00aba387b0b0"),
"tenantID" : 1,
"date" : "2016-10-09 00:23:56",
"venueList" : [
{
"id" : “VID1212”,
"sum" : [
{
"name" : "linux",
"value" : 12
},
{
"name" : "ubuntu",
"value" : 4
}
],
“ssidList” : [ // this is list of ssid’s in venue
{
"id" : “SSID1212”,
"sum" : [
{
"name" : "linux",
"value" : 8
},
{
"name" : "ubuntu",
"value" : 6
}
],
“macList” : [ // this is mac list inside particular ssid ex: this is mac list inside the SSID1212
{
"id" : “12:12:12:12:12:12”,
"sum" : [
{
"name" : "linux",
"value" : 12
},
{
"name" : "ubuntu",
"value" : 1
}
]
}
]
}
]
},
{
"id" : “VID4343”,
"sum" : [
{
"name" : "linux",
"value" : 2
}
],
"ssidList" : [
{
"id" : “SSID4343”,
"sum" : [
{
"name" : "linux",
"value" : 2
}
],
"macList" : [
{
"id" : “43:43:43:43:43:34”,
"sum" : [
{
"name" : "linux",
"value" : 2
}
]
}
]
}
]
}
]
}
我实现golang代码如下
db.useragents.aggregate([
{ "$match": { "venueList.id": { "$in": ["VID1212", "VID4343"] } } },
{ "$unwind": "$venueList" },
{ "$match": { "venueList.id": { "$in": ["VID1212", "VID4343"] } } },
{ "$unwind": "$venueList.sum" },
{
"$group": {
"_id": "$venueList.sum.name",
"count": { "$sum": "$venueList.sum.value" }
}
},
{
"$group": {
"_id": null,
"counts": {
"$push": {
"name": "$_id",
"value": "$count"
}
}
}
}
])
我创建了一个pipline并将其提供给pipe.All()但是从func GetBrowserStats(constrains models.Constrains) ([]bson.M, error) {
session := commons.GetMongoSession()
defer session.Close()
var col = session.DB("analytics").C("useragents")
pipeline1 := bson.M{
"$match": bson.M{
"venueList.id": bson.M{
"$in": []string{"VID1212", "VID4343"},
},
},
}
pipeline2 := bson.M{
"$unwind": "$venueList",
}
pipeline3 := bson.M{
"$match": bson.M{
"venueList.id": bson.M{
"$in": []string{"VID1212", "VID4343"},
},
},
}
pipeline4 := bson.M{
"$unwind": "$venueList.sum",
}
pipeline5 := bson.M{
"$group": bson.M{
"_id": "$venueList.sum.name",
"count": bson.M{
"$sum": "$venueList.sum.value",
},
},
}
pipeline6 := bson.M{
"$group": bson.M{
"_id": bson.NewObjectId(),
"counts": bson.M{
"$push": bson.M{
"name": "$_id",
"value": "$count",
},
},
},
}
all := []bson.M{pipeline1, pipeline2, pipeline3, pipeline4, pipeline5, pipeline6}
pipe := col.Pipe(all)
result := []bson.M{}
err := pipe.All(&result)
println(result[0])
if err != nil {
println(err.Error())
errMsg := "Error occourred while getting dashboard configs from mongo stack:" + err.Error()
log.Error()
return result, errors.New(errMsg)
}
return result, nil
}
变量返回null结果。
我想在result
result
答案 0 :(得分:1)
最后我找到了解决方案。我想与stackoverflow社区分享。
我推荐这个:google group answer
session := commons.GetMongoSession()
defer session.Close()
pipeline := []bson.D{
bson.D{
{"$match",
bson.M{
"venueList.id": bson.M{"$in": []string{"VID1212", "VID4343"}},
},
},
},
bson.D{
{"$unwind", "$venueList"},
},
bson.D{
{"$match",
bson.M{
"venueList.id": bson.M{"$in": []string{"VID1212", "VID4343"}},
},
},
},
bson.D{
{"$unwind", "$venueList.sum"},
},
bson.D{
{"$group",
bson.M{
"_id": "$venueList.sum.name",
"count": bson.M{
"$sum": "$venueList.sum.value",
},
},
},
},
bson.D{
{"$group",
bson.M{
"_id": bson.NewObjectId(),
"counts": bson.M{
"$push": bson.M{
"name": "$_id",
"value": "$count",
},
},
},
},
},
}
query := bson.D{
{"aggregate", "useragents"}, // useragents is a collection name
{"pipeline", pipeline},
}
var res interface{}
err := session.DB("analytics").Run(query, &res)
if err != nil {
println(err.Error())
} else {
println(res)
}