Mongodb is dynamic as an operator

In mongodb, the equivalent of the sql operator is "like"

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

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

I tried

letter = req.params.letter;

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

This does not work, I think, because now slashes are interpreted differently.

+5
source share
2 answers

One way to do this, according to the page :

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

+1 for mindandmedia on syntax. However, remember that if you want the query to use the index efficiently , you need to use prefix queries (also called root regular expressions), for example, the prefix / ^ /

, , - . :

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

+2

All Articles