For example if my keyword is "all", I would like MongoDb to return all the documents in the given field containing the words "all" like "edgar allan poe" or "whitney hall". How do I do that in MongoDb? I've been stuck here for days so any help would be greatly appreciated :)
For example if my keyword is "all", I would like MongoDb to return all the documents in the given field containing the words "all" like "edgar allan poe" or "whitney hall". How do I do that in MongoDb? I've been stuck here for days so any help would be greatly appreciated :)
Share Improve this question asked Jun 28, 2013 at 3:40 user1710631user1710631 1131 gold badge1 silver badge5 bronze badges1 Answer
Reset to default 18By using a regular expression. MongoDb has a $regex operator.
db.authors.find({name: {$regex: 'all', $options: 'i'}});
Regex operations cannot use indexes, so find() operations won't be efficient. An index can only be used for prefix(starts with) and case-sensitive matches like this query:
db.authors.find({name: {$regex: '^a'});