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
  • 4,567
  • 13
  • 30
  • 40
Alexander Ceballos
  • 730
  • 2
  • 19
  • 35
  • 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

16

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,165
  • 4
  • 25
  • 43
Alexander Ceballos
  • 730
  • 2
  • 19
  • 35