Friday 14 April 2017

mongodb text search partial match

Hii All I want to perform like query with two different search terms with two different fields .But none of my code works .Please help . I want to perform below sql query in mongodb or mongoose:

SELECT * FROM Customers
WHERE title LIKE '%$title%' OR location LIKE '%$location%';

I have tried with the codes given below but none of these working at all:

         ***1. First Code***
var searchString1 = req.body.location;
  var searchString2 = req.body.title;

Customer.find({$or:{ "location": /searchString1/ },{ "title": /searchString2/ }]}).exec(function(err,jobs){}

         ***2. Second Code***
Customer.find({"$or":[
                {"title":new RegExp(req.body.title,'i')},
                {"location":new RegExp(req.body.location,'i')}
            ]}).exec(function(err,jobs){}

Please Help



via Bipin Swarnkar

No comments:

Post a Comment