mongodb like search

http://stackoverflow.com/questions/3305561/how-do-i-query-mongodb-with-like

  • db.users.find({"name": /.*m.*/})

    or, similar,

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

    You’re looking for something that contains “m” somewhere (SQL’s ‘%’ operator is equivalent to regexp’s ‘.*’), not something that has “m” anchored to the beginning of the string.

  • is searching by regex expensive?
    • Actually, it depends. If the query doesn’t use an index, and must do a table scan, then it can certainly be expensive. If you’re doing a ‘starts with’ regex query, then that can use an index. Best to run an explain() to see what’s happening. – Kyle Banker Jul 22 ’10 at 14:49
    • When not anchored to the beginning of the string, it is somewhat expensive. But then again, so is a LIKEquery in SQL. – Emily Jul 26 ’10 at 18:50
    • so as long as it’s anchored to the beginning of the string it’s okay? cool then. does that mean we need to add ^ – Jim Thio Aug 31 ’12 at 3:55
    • I would add regex i javascript db.users.find({ "name": { $regex: /m/i } }) – Doron Segal Jun 19 ’15 at 3:24
Advertisements
This entry was posted in DB, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s