我正在尝试使用 golang mgo 包执行查询以有效地从 $group 聚合中获取相似值并计算总记录值。但我无法获取分组数据的总记录数。我的结构是这样的:
{
"data": [
{
"_id": 366,
"logs": [
{
"id": 113,
"booking_id": 366,
"provider_id": 13,
"cid": 11,
"type": "basic",
"time": 1542793756,
},
{
"id": 116,
"booking_id": 366,
"provider_id": 13,
"cid": 0,
"type": "type2",
}
]
},
{
"_id": 362,
"logs": [
{
"id": 104,
"booking_id": 362,
"provider_id": 7,
"cid": 10,
"type": "basic",
"time": 1542776677,
}
]
},
{
"_id": 370,
"logs": [
{
"id": 111,
"booking_id": 370,
"provider_id": 9,
"cid": 11,
"type": "basic",
"time": 1542792661,
},
{
"id": 112,
"booking_id": 370,
"provider_id": 11,
"cid": 11,
"type": "basic",
"time": 1542793185,
}
]
}
],
"total_record": 2
}
为此,我想要在数据中预设总记录::
"total_record":3
查询我正在使用 ::
query := []bson.M{
{"$group": bson.M{
"_id": "$booking_id",
"logs": bson.M{ "$push": "$$ROOT" }
"count": bson.M{"$sum":1},
}},
}
pipe := getCollection.Pipe(query)
err = pipe.AllowDiskUse().One(&result)
我想要这个结果的总数。日志部分包含具有不同“provider_id”值的重复预订数据,但我已将所有类似的 booking_id 数据分组在单个文档中并显示它的计数。$group 聚合有可能吗?
侃侃尔雅
相关分类