https://godoc.org/github.com/mongodb/mongo-go-driver我正在嘗試動態創建聚合管道。例如,我想讀取一段包含海洋的字符串。我試著把它們拆成碎片,但我找不到任何方法來追加元素。pipeline := bson.NewArray( bson.VC.DocumentFromElements( bson.EC.SubDocumentFromElements( "$match", bson.EC.SubDocumentFromElements("ocean", bson.EC.ArrayFromElements("$in", bson.VC.String("Pacific Ocean"), //bson.VC.String("Indian Ocean"), ), ), bson.EC.SubDocumentFromElements("callTypeName", bson.EC.ArrayFromElements("$in", bson.VC.String("Wookie"), bson.VC.String("Unknown 13"), ), ), ), ),)cur, err := collection.Aggregate(context.Background(), pipeline)
1 回答

偶然的你
TA貢獻1841條經驗 獲得超3個贊
這個人要問的是在給定數據列表的情況下將數據動態插入到管道中。
我和我的團隊正在開發的 vue 應用程序也遇到了同樣的問題。使用您提供的數據,這是通用模板:
給定一片海洋
a?:=?[]string{"Pacific?Ocean",?"Indian?Ocean"}
制作一個類型為 *bson.Value 的大小為 0 的切片
b?:=?make([]*bson.Value,?0)
遍歷海洋切片并將 bson 轉換后的值附加到切片 b
for?_,?v?:=?range?a?{ ????b?=?append(b,?bson.VC.String(v)) }
然后創建鍵值對,以便 mongo 可以查找匹配項
c?:=?bson.EC.ArrayFromElements("$in",?b...)
然后將 c 傳遞到管道中
pipeline?:=?bson.NewArray( ????bson.VC.DocumentFromElements( ????????bson.EC.SubDocumentFromElements( ????????????????????"$match", ????????????bson.EC.SubDocumentFromElements("ocean",?c), ????????), ????), )
這應該讓您了解如何為 callTypeNames 動態管道
- 1 回答
- 0 關注
- 135 瀏覽
添加回答
舉報
0/150
提交
取消