最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I make searches on Mongodb using wildcard characters? - Stack Overflow

programmeradmin4浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 18

By 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'});
发布评论

评论列表(0)

  1. 暂无评论