如何在mgo中进行文本搜索?

Cha*_*aju 5 go mongodb mgo

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

c.Find(bson.M{"$text": bson.M{"abc": "efg"}})
Run Code Online (Sandbox Code Playgroud)

c是Collection对象。我没有任何结果。我究竟做错了什么?

Goo*_*ine 5

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

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

c.EnsureIndexKey("abc")
c.Find(bson.M{"$text": bson.M{"$search": "efg"}})
Run Code Online (Sandbox Code Playgroud)

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