How I can use "LIKE" operator on mongoose?

Or just simply

const name = "John"
UserSchema.find({name: {$regex: name, $options: 'i'}}).limit(5);

i for case-insensitive


use $regex in mongodb

how to use regex

example

select * from table where abc like %v%

in mongo

 var colName="v";
 models.customer.find({ "abc": { $regex: '.*' + colName + '.*' } },
   function(err,data){
         console.log('data',data);
  });

Your query look like

var name="john";
UserSchema.find({name: { $regex: '.*' + name + '.*' } }).limit(5);

This is how i have done it to find if search string present in name/email. Hope it helps someone.

const getPeerSuggestions = async (req, res, next) => {

  const { search } = req.query;
  const rgx = (pattern) => new RegExp(`.*${pattern}.*`);
  const searchRgx = rgx(search);

  const peers = await User.find({
    $or: [
      { name: { $regex: searchRgx, $options: "i" } },
      { email: { $regex: searchRgx, $options: "i" } },
    ],
  })
    .limit(5)
    .catch(next);

  res.json(peers);
};