How to find elements using regexes in Mongoose

In the Mongoose doc, I did not find the equivalent for $ regex in MongoDb. Can you provide a simple Mongoose find()with a regex expression?

+4
source share
1 answer

mongoose doc for search .

mongodb doc for regex .

   var Person = mongoose.model('Person', yourSchema);
   // find each person with a name contains 'Ghost'
   Person.findOne({ "name" : { $regex: /Ghost/, $options: 'i' } },
          function (err, person) {
                 if (err) return handleError(err);
                 console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation);

   });

, mongoose.findOne. "{" name ": {$ regex:/Ghost/, $options: 'i'}}". "" - , . "" - . "i" - , . , .

+12

Source: https://habr.com/ru/post/1648617/


All Articles