Mongoose: request to run with

I want to basically search by username.

User.find({ username: "Mich"}) 

I need a query like the one above that will return all users whose username begins with "Mich". Michael, Michaela, Michijger et al.

+5
source share
1 answer

You can search with regex , this should work in Node

 User.find({ username: /^Mich/}) 

Note that Mongo supports regex objects, which means you can do

 var regexp = new RegExp("^"+ req.params.username); User.find({ username: regexp}); 

or Mongos native regex constructor

 User.find({ username: {$regex : "^" + req.params.username}}); 
+23
source

All Articles