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)发布于 2018-11-13 01:10:15
我认为这个问题很清楚,不确定第一个评注是否真的仔细阅读了声明。
这个人要求的是动态地将给定的数据列表插入到管道中。
我和我的团队在一个vue应用程序上也有同样的问题。使用您提供的数据,以下是通用模板:
给出一片海洋
a := []string{"Pacific Ocean", "Indian Ocean"}制作大小为0的*bson.Value类型的切片
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进行管道处理。
https://stackoverflow.com/questions/53271818
复制相似问题