golang mongodb Aggregate

前端之家收集整理的这篇文章主要介绍了golang mongodb Aggregate前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
q := []bson.M{
{ "$group": bson.M{
"_id": bson.M{ "sessionid": "$sessionid"},
"sessionid": bson.M{ "$first": "$sessionid"},
"des": bson.M{ "$first": "$des"},
"serial": bson.M{ "$first": "$serial"},
"timestamp": bson.M{ "$first": "$timestamp"},
"timestring": bson.M{ "$first": "$timestring"}}},

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

ss,err := db. AggregatePhonePerf(q)
if err != nil {
log. Error(err)
ret := map[ string] interface{}{ "code": "006500","msg": err}
return c. JSON( 500,ret)
}

//AggregatePhonePerf

func AggregatePhonePerf(pipeline interface{}) (results []SerialSession,err error) {
exop := func(c *mgo.Collection) error {
log. Info(pipeline)
return c. Pipe(pipeline). All(&results)
}
err = getCollection( "phonePerf",exop)
return
}


func getProp(d interface{},label string) ( interface{}, bool) {
switch reflect. TypeOf(d). Kind() {
case reflect.Struct:
v := reflect. ValueOf(d). FieldByName(label)
return v. Interface(),true
case reflect.Map:
_map, ok := d.( map[ string] interface{})
if ok {
log. Info(_map,ok)
v := _map[label]
if v != nil {
return v, true
}
} else {
__map, _ok := d.(bson.M)
log. Info(__map,_ok)
v := __map[label]
if v != nil {
return v, true
}
}
}
return nil,false
}
原文链接:https://www.f2er.com/go/188254.html

猜你在找的Go相关文章