CarlP
CarlP

Reputation: 201

Multiple property query in MongoDB using Mongoose

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

Answers (1)

Soroush Salehi 404
Soroush Salehi 404

Reputation: 323

as Cuong Le Ngoc said you can use $or and also $regex

how to use regex

how to use or

var search="ar";
yourCollectionSchema.find({ $or: [{firstName: { $regex: '.*' + search+ '.*' } },lastName: { $regex: '.*' + search+ '.*' }] })

Upvotes: 1

Related Questions