Reputation: 201
I'm using mongoDB & Mongoose.
I would like to query in my collection of the string provided by the user is contained in either of the two property I have on each document so for example:
query string is :"ar"
{ firstName:"Mark", lastName:"Sashloe" },
firstName:"Kevin", lastName:"Charloom" }
in my proposed query, both documents should be catched since the firstName property of the first document contains "ar" and the lastName property of the second document has it too.
Upvotes: 0
Views: 333
Reputation: 323
as Cuong Le Ngoc said you can use $or and also $regex
var search="ar";
yourCollectionSchema.find({ $or: [{firstName: { $regex: '.*' + search+ '.*' } },lastName: { $regex: '.*' + search+ '.*' }] })
Upvotes: 1