How to find all the results containing a given substring with spaces in mongoose How to find all the results containing a given substring with spaces in mongoose mongoose mongoose

How to find all the results containing a given substring with spaces in mongoose


It's most probably because you are not converting the whitespaces into "\s" (\s+ is better though) character like

"$regex": new RegExp(partialToSearch.replace(/\s+/g,"\\s+"), "gi");

I haven't had the chance to try it out with mongoose but i am pretty sure it's fine.


Your research is very useful I found my answer from your question and I just read Mongoose and MongoDb documentation and got answerhttps://docs.mongodb.com/manual/reference/operator/query/regex/#op._S_regex

Vehicle.find({ registrationNo: { "$regex": registrationNo, "$options": "ix" }}, function (err, vehicle) {}// { <field>: { $regex: /pattern/, $options: '<options>' } }

Just provided options (i) will ignore case and (x) will truncate spaces from pattern

Screenshot from MongoDb documentation:

enter image description here