golang mongodb Aggregate
发布时间:2020-12-16 18:10:17 所属栏目:大数据 来源:网络整理
导读: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{ "$firs
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
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |