gpt4 book ai didi

mongodb - 使用 mongo-go-driver 创建动态过滤器

转载 作者:数据小太阳 更新时间:2023-10-29 03:19:04 25 4
gpt4 key购买 nike

我想用 mongo-go-driver 创建一个动态过滤器。

例如,我有这个文档:

{
"_id":"5d1231380a2a2b39a99c3ed1"},
"name":"flower.png",
"colors":["#ffffff","#212121","#999999","#dbdbdb","#ff5252"],
},
{
"_id":"5d1231380a2a2b39a99c3ed0"},
"name":"image of go.jpg",
"colors":["#dedede","#dfdfdf","#dddddd","#e0e0e0","#e2e2e2"],
},
{
"_id":"5d1231380a2a2b39a99c3ecf"},
"name":"bolket rulez.png",
"colors":["#000000","#010101","#020202","#030303","#040404"],
},
{
"_id":"5d1231380a2a2b39a99c3ecf"},
"name":"bolket photo.png",
"colors":["#ffffff","#010101","#020202","#030303","#040404"],
}

现在我想要所有名称中包含“bolket”且颜色为“#ffffff”的文档。

我试过这个过滤器:

filter := make(bson.D, 2)

if f.Name != "" {
filter = append(filter, bson.E{Key: "name", Value: bson.M{"$text": bson.M{"$search": f.Name}}})
}

if f.Color != "" {
filter = append(filter, bson.E{Key: "colors", Value: bson.M{"$in": f.Color}})
}

但是使用这个过滤器我有这个错误:(BadValue) unknown operator: $text

如何创建动态过滤器???

最佳答案

query op $text 在文档中 文本索引字段搜索,您不能使用$text 进行单个字段搜索

假设您已经在 name 上创建了一个文本索引,然后尝试:

  if f.Name != "" {
filter = append(filter, bson.E{Key: "$text", Value: bson.M{"$search": f.Name}})
}

关于mongodb - 使用 mongo-go-driver 创建动态过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56776608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com