Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
schema

person = {
name : String
}


Have a long 500+ list of names ['Name1', 'Name2', 'Name3', 'Name4', 'Name5', 'Name6' ...]

How to write better mongo find query to search name from a list

What I have tried:

db.person.find({ $or: [{"name": 'Name1'},{"name": 'Name2'}, {"name": 'Name3'}, {"name": 'Name4'},{"name": 'Name5'}, {"name": 'Name6'}]},{})
Posted
Updated 28-Nov-16 6:44am
v3

1 solution

If I understand correctly, you want to find several users based on their usernames. You can use this syntax for that:
db.person.find({ name: { $in: [ "Name1", "Name2", "Name3" ] } })

More about the $in operator: $in — MongoDB Manual 3.2[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900