5

How can I do a like query in MongoDB using Node.js?

Here's my source code, but it doesn't work:

exports.findAll = function(req, res) {
    var country=req.params.country; 
    db.collection('wines', function(err, collection) {
        collection.find({ 'country': new RegExp('/'+country+'/i') }).toArray(function(err, items) {
            res.jsonp(items);
        });
    });
};
double-beep
  • 5,031
  • 17
  • 33
  • 41
Alexander Ceballos
  • 750
  • 2
  • 20
  • 36
  • possible duplicate of [Mongoose find query doesn't work even equivalent query works fine on mongodb](http://stackoverflow.com/questions/14804168/mongoose-find-query-doesnt-work-even-equivalent-query-works-fine-on-mongodb) – JohnnyHK Jun 27 '13 at 22:04
  • Heres I found the solution: http://stackoverflow.com/questions/14804168/mongoose-find-query-doesnt-work-even-equivalent-query-works-fine-on-mongodb – Alexander Ceballos Jun 27 '13 at 22:13
  • you can find here: https://stackoverflow.com/a/71136307/14229690 – Sahil Thummar Apr 30 '22 at 17:21

1 Answers1

17

I solved it like this:

 exports.findAll = function(req, res) {
        var country=req.params.country; 
        db.collection('wines', function(err, collection) {
            collection.find({ 'country': new RegExp(country, 'i') }).toArray(function(err, items) {
                res.jsonp(items);
            });
        });
    };
Ethan
  • 4,295
  • 4
  • 25
  • 44
Alexander Ceballos
  • 750
  • 2
  • 20
  • 36