mamadgi aishwarya
mamadgi aishwarya

Reputation: 107

Text Search in the database mongoose

I performed text search like this:

Post.find( { $text: { $search: "hello" } }) 
      .then(products => console.log(products))
      .catch(e => console.error(e));

It is finding the object that has text "Hello World" but not "hello3". I want it to output Hello World and hello3. How can I do that in mongoose? Please help.Thanks in advance.

Upvotes: 0

Views: 323

Answers (2)

mohamed gamal
mohamed gamal

Reputation: 26

you can use regex search like:

db.collectionname.find({message:new RegExp( 'hello' , 'i')})

and you can use .trim() for your query search value :)

Upvotes: 1

JavaProgrammer12
JavaProgrammer12

Reputation: 176

I don't think text index can do this. You may want to try $regex search. Something like below

db.collectionname.find({"message": {"$regex" :"Hello.*",$options: 'i'} })

Upvotes: 1

Related Questions