I have a problem when i query using mongoose.I coding follow this Mongoose.js: Find user by username LIKE value. But it return blank.
This my code return blank.
var promise = UserSchema.find({name: /req.params.keyword/ }).limit(5);
I tried this return blank seem.
var n = john; var promise = UserSchema.find({name: /n/ }).limit(5);
But i tried this is working
var promise = UserSchema.find({name: /john/ }).limit(5);
Why I use variable then return blank?
The lean() function tells mongoose to not hydrate query results. In other words, the results of your queries will be the same plain JavaScript objects that you would get from using the Node. js MongoDB driver directly, with none of the mongoose magic.
In mongoose, exec method will execute the query and return a Promise.
The value of the $in operator is an array that contains few values. The document will be matched where the value of the breed field matches any one of the values inside the array.
Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.
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);
Or just simply
const name = "John" UserSchema.find({name: {$regex: name, $options: 'i'}}).limit(5);
i for case-insensitive
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With