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

javascript - Mongodb dynamic like operator - Stack Overflow

programmeradmin6浏览0评论

In mongodb the equivalent to sql "like" operator is

db.users.find({"shows": /m/})

Using nodejs/javascript I want to dynamically change letter, based on url paramater.

I have tried

letter = req.params.letter;

db.users.find({"shows": '/' + letter + '/'})

This doesn't work, I guess because the slashes are now strings are interpreted differently.

In mongodb the equivalent to sql "like" operator is

db.users.find({"shows": /m/})

Using nodejs/javascript I want to dynamically change letter, based on url paramater.

I have tried

letter = req.params.letter;

db.users.find({"shows": '/' + letter + '/'})

This doesn't work, I guess because the slashes are now strings are interpreted differently.

Share Improve this question asked Jul 6, 2012 at 12:21 jamjamjamjam 3,2697 gold badges35 silver badges39 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 16

One way to do it, according to the documentation page:

db.users.find( { shows : { $regex : letter } } );

+1 for mindandmedia on the syntax. However, please remember, that if you want the query to use an index efficiently, you have to use prefix queries (also called rooted regexps) like /^prefix/

Your query is likely to be horribly slow otherwise - see the note in the docs here:

http://www.mongodb/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions

You can try this:

let filter = "Dynamic";

let str = /.*Raj.*/;
console.log(str);
console.log(typeof(str));


let stra = eval(`/.*${filter}+.*/`);
console.log(stra);
console.log(typeof(stra));
发布评论

评论列表(0)

  1. 暂无评论