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?
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/
use $regex(option i for case insensitive)
example:
c.Find(bson.M{"abc": &bson.RegEx{Pattern: "efg", Options: "i"}})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With