1

Currently, I have the following route for a text search.

router.post("/visit", middleware.isLoggedin, function(req,res){
    subject.find({Subject_Name: req.body.user} ).sort('Visit_date').exec(function(err,returnedsubjects){
        if(err){
            console.log(err);
        } else {
            console.log(returnedsubjects);
            res.render("visit",{returnedsubjects: returnedsubjects);
        }
    });
});

It works fine. But as you can see the subject_name has to match exactly. I need "david" or "David" to find collections where the Subject_Name = "David Warner" for example.

Tried this suggestion. How to query MongoDB with "like"?. Did not work.

Tried this subject.find({Subject_Name: /req.body.user/} ) and this subject.find({"Subject_Name": /req.body.user/} )

no luck :(.

Any help is appreciated. Thanks!

Rainmaker
  • 321
  • 3
  • 15

1 Answers1

2

Create a text index on the Subject_Name field of our document using the following query :

subject.index({Subject_Name:"text"})

then you can search using $text and $search like this :

 var text = req.body.user;
    subject.find({ $text:{$search: text} });

So let's say that you have created the following Schema :

  var subjectschema = new mongoose.Schema({ Subject_Name: { type: String, index: true },etc)}

you have to create the index in the schema's file so your schema file will look like this :

     var subjectschema = new mongoose.Schema({ 
     Subject_Name: { type: String, index: true },
     etc
     )};

    subjectschema.index({Subject_Name:"text"};

then start searching in the route :

 subjectschema.find( {$text:{$search: req.body.user} }).then(result=>{
        if (result)
        {
           //do something with your code
            console.log(result);
        }

    })

So now if we are searching for "david" the above query returns the following documents containing the keyword "david" in their Subject_Name field.

anehme
  • 536
  • 1
  • 6
  • 18
  • Thanks. I will try this. Whats the best place to do the indexing. Should I add that in the route (in the original post). I have a seed.js file that I use to seed the database when the application starts up. Should I use that instead for ```subject.createIndex({Subject_Name:"text"})``` – Rainmaker Oct 11 '19 at 16:30
  • or in the subject model? – Rainmaker Oct 11 '19 at 16:35
  • Tried creating the index on the subject Model. like this ```var subjectschema = new mongoose.Schema({ Subject_Name: { type: String, index: true }, ...etc)```. No luck. In the route when I call ```subject.find({ $text:{$search: text}})```, I get the error ```MongoError: text index required for $text query``` – Rainmaker Oct 11 '19 at 16:56
  • Thanks. Got this to work and marked it as the right answer. Do you know how I can make it "like" search. As in where "dav" or "war' can return the match "David Warner" - similar to "like '%dav%'" in SQL? – Rainmaker Oct 11 '19 at 19:06