dongrang2140 2014-05-20 13:50
浏览 35
已采纳

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

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?

  • 写回答

2条回答 默认 最新

  • dou5454954610 2014-05-20 14:43
    关注

    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/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?