dtqu72830 2019-06-26 15:44
浏览 183

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

I want create a dynamic filter with the mongo-go-driver.

For example, I have this documents:

{
"_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"],
}

Now I want all documents that have "bolket" in the name and "#ffffff" color.

I have tryed with this filter:

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}})
    }

But with this filter I have this error: (BadValue) unknown operator: $text

How can I create a dynamic filter???

  • 写回答

1条回答 默认 最新

  • duankua3620 2019-06-29 12:40
    关注

    query op $text search across text index fields on a doc, you can't do single field search with $text

    assuming you have created a text index on name, then try:

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

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置