Home > Back-end >  Node js mongoDB query for search exact word case insensitive
Node js mongoDB query for search exact word case insensitive

Time:12-18

  const searchedWord = req.body.searchTerm;
  console.log(searchedWord);

  db.collection('subtitle')
    .find({
      $text: { $search: searchedWord },
    })

Here is my code it takes a search word coming from the user and searches through all documents and returns the results. but the thing is it is case sensitive plus returns all the documents containing the world. if you search for "happen", some other words like "happened" and "happens" also return. I just want to make it case insensitive and exact word. I used regex but it does not work when my entry is dynamic like this. all the MongoDB documentation is about a hardcoded word for search.

CodePudding user response:

MongoDB Text Indexes use language-specific stemming rules.

When using english, suffixes are remove and the stem word is indexed, so "happens", "happened", and "happening" are all stored in the index as "happen".

To disable stemming, explicitly specify the language as "none".

  • Related