2 回答

TA贡献1853条经验 获得超18个赞
除了解析一串MongoDB 扩展 JSON来构建聚合管道之外,您还可以构建一个bson.Array对象(类型化):
例如:
pipeline := bson.NewArray(
bson.VC.DocumentFromElements(
bson.EC.SubDocumentFromElements(
"$group",
bson.EC.SubDocumentFromElements(
"_id",
bson.EC.String("ProductCode","$ProductCode"),
bson.EC.String("Dir","$Dir"),
bson.EC.String("WharehouseID","$WharehouseID"),
),
),
),
)
cursor, err := collection.Aggregate(context.Background(), pipeline)
上面的代码片段与当前的mongo-go-driver 版本 0.0.12兼容

TA贡献1830条经验 获得超9个赞
我得到了它!
我犯了两个错误:
1 - 我必须解析一个 JSON 对象数组
2 - 关闭“`”之前没有换行
这是工作示例:
group, e := bson.ParseExtJSONArray(`[{
"$group": {
"_id":{
"ProductCode":"$ProductCode",
"Dir":"$Dir",
"WharehouseID":"$WharehouseID"
}
}
}]`)
cursor, e := myCollection.Aggregate(
context.Background(),
group,
)
- 2 回答
- 0 关注
- 241 浏览
添加回答
举报