mongodb - 如何在 mgo bson 中聚合字段类型 map[string]uint32?
问题描述
我正在尝试使用 bson 和 Golang 从 mongo 集合中获取聚合值,其中 Golang 中使用的字段是 type map[string]uint32
。
这是正在使用的结构,字段是Csat
.
type Conversations struct {
ID bson.ObjectId `bson:"_id"`
Csat map[string]uint32 `bson:"csat"`
}
使用 bson 的输出应该是键值对,这些是我尝试过但没有用的一些示例,
1.
bson.M{
"csat["1"]": bson.M{
"$sum": bson.M{"$cond": []interface{}{bson.M{"$eq": []interface{}{"$score", 1}}, 1, 0}},
},
"csat["2"]": bson.M{
"$sum": bson.M{"$cond": []interface{}{bson.M{"$eq": []interface{}{"$score", 2}}, 1, 0}},
}
2.
bson.M{
"csat": bson.M{
"1": bson.M{
"$sum": bson.M{"$cond": []interface{}{bson.M{"$eq": []interface{}{"$score", 1}}, 1, 0}},
},
"2": bson.M{
"$sum": bson.M{"$cond": []interface{}{bson.M{"$eq": []interface{}{"$score", 2}}, 1, 0}},
},
}
}
我希望它将它存储{"csat": {"1": 3}, {"2": 2}, ...}
在 3 和 2 可能是键值的位置。
如果有人以前做过这样的事情,你能帮帮我吗?
谢谢。