MongoDB: How to avoid name doubles in collection
You can do this:
Customer.find({ name: { $regex: new RegExp("^" +name.replace(/\s/g, ''), "i") } })
but since mongoose 3.4
, it is recommended to use case sensitive index
: documentation here
so you can simply do:
Customer.find({ name: name.replace(/\s/g, '') }) .collation( { locale: 'en', strength: 2 } )