5

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

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

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

4

2 回答 2

4

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

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

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

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

于 2014-05-20T14:43:23.777 回答
2

use $regex(option i for case insensitive)
example:

c.Find(bson.M{"abc": &bson.RegEx{Pattern: "efg", Options: "i"}})
于 2014-05-21T11:45:57.517 回答