I run following query in mongo shell:
db.Profiles.find ( { $or : [ { "name" : "gary" }, {"name":"rob} ] } )
It just returns nothing as expected(JSON)?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use $in
For the query in the question, it’s more appropriate to use $in
Why doesn’t it work
There’s a missing quote – the cli is waiting for you to finish the second part of your or:
You need to finish the query sufficiently for the cli to parse it for it to then say there’s a syntax error.
Case insensitive matching
As indicated by a comment, if you want to search in a case insensitive manner, then you either use
$orwith a $regex:Or, you simply use one regex:
However, a regex query that doesn’t start with a fixed string cannot use an index (it cannot use an index and effectively do “start here until no match found then bail”) and therefore is sub-optimal. If this is your requirement, it’s a better idea to store a normalized name field (e.g.
name_lc– lower case name) and query on that: