How to do text search in mgo?

心不动则不痛 提交于 2019-12-23 09:58:13

问题


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

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

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


回答1:


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

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/




回答2:


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

c.Find(bson.M{"abc": &bson.RegEx{Pattern: "efg", Options: "i"}})


来源:https://stackoverflow.com/questions/23761689/how-to-do-text-search-in-mgo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!