如何在mgo中进行文本搜索? [英] How to do text search in mgo?

查看:64
本文介绍了如何在mgo中进行文本搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在名为"abc"的字段中搜索"efg"

I'm trying to search "efg" in field named "abc"

c.Find(bson.M{"$text": bson.M{"abc": "efg"}})

c是Collection对象.我没有任何结果.我在做什么错了?

c is Collection object. I'm not getting any result. What am I doing wrong?

推荐答案

您正在生成 {$ text:{abc:"efg"}} ,但是您的查询应如下所示: {$ text:{$ search:"efg"}}

You are generating {$text:{abc:"efg"}}, but your query should look like this: {$text:{$search:"efg"}}

因此请尝试将代码更新为:

So try updating your code to:

c.EnsureIndexKey("abc")
c.Find(bson.M{"$text": bson.M{"$search": "efg"}})

请记住,要使用 $ text 进行搜索,您需要指定一个索引.请查看说明如何使用该文档的文档: http://docs.mongodb.org/manual/reference/operator/query/text/

Keep in mind that to search with $text, you need to specify an index. Check out this document that explains how to use it: http://docs.mongodb.org/manual/reference/operator/query/text/

这篇关于如何在mgo中进行文本搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆