2 回答

TA貢獻1853條經(jīng)驗 獲得超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條經(jīng)驗 獲得超9個贊
我得到了它!
我犯了兩個錯誤:
1 - 我必須解析一個 JSON 對象數(shù)組
2 - 關閉“`”之前沒有換行
這是工作示例:
group, e := bson.ParseExtJSONArray(`[{
"$group": {
"_id":{
"ProductCode":"$ProductCode",
"Dir":"$Dir",
"WharehouseID":"$WharehouseID"
}
}
}]`)
cursor, e := myCollection.Aggregate(
context.Background(),
group,
)
- 2 回答
- 0 關注
- 213 瀏覽
添加回答
舉報