Mongoose.js find with regex

When consuming MongoDB with mongoose helper functions like find() in NodeJS, it is easy to filter with a regular expression.

The end result will be similar to using a  “LIKE” query in SQL.

How to use regex with the Find function

var name = 'Peter';
model.findOne({name: new RegExp('^'+name+'$', "i")}, function(err, doc) {
  //Do your action here..

Doing this essentially queries mongodb with this regular expression: /Peter/i

For simpler regular expressions not requiring the passage of a dynamic variable, you can use a regex expression straight into the find filter object:

model.find({'name' : /stuff/i}, function(err, docs){

Source: StackOverflow