Open In App

Search Text in MongoDB

Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB provides a good technique that is text search. Using this technique we can find a piece of text or a specified word from the string fields. Or in other words, MongoDB allows you to perform a query operation to find the specified text from the string. In MongoDB, we can perform text search using text index and $text operator.

Text index: MongoDB proved text indexes that are used to find the specified text from the string content. Text indexes should be either a string or an array of string elements. When you perform a text search query always remember that your collection must contain a text index and a collection can only contain one text index but this single text index covers multiple fields. We can create a text index using createIndex() method. 

Syntax:

db.collectionName.createIndex( { field: “text” } )

$text Operator: You can also search text index using the $text operator. This operator is used to perform text search operations on the collection with a text index. This operator tokenized each search string with whitespace and it treats most punctuations as delimiters except – and \”. After tokenizing the search string it performs logical OR operation on the tokens. If you want to sort the resultant documents then use the $meta query operator.

Syntax:

$text:

{

     $search: <string>,

     $language: <string>,

     $caseSensitive: <boolean>,

     $diacriticSensitive: <boolean>

}

How to search text?

Using the following steps we can search text in MongoDB:

Step 1: First, we create a collection and add some documents to the collection:  

In the following example, we are working with:

Database: gfg

Collection: content

Document: The content collection contains the three documents.

Step 2: Create index:

Now we create a string index on the name and pet field with the help of the createIndex() method. So we can search text over the name and line fields:

db.content.createIndex({name:"text",line:"text"})

Step 3: Search Text:

Now we are ready to search text. For example, we are going to search all the document which contain text love.

db.content.find({$text:{$search:"love"}})  

Note: $text is a query operator that performs text searches on a collection with a text index.

One more example in which we are going to search all the document which contain dog text:

db.content.find({$text:{$search:"dog"}})

How to search phrases?

Instead of searching a single word you are also allowed to search for a phrase by wrapping the phrase in double quotes(“”). Generally, the phrase search performs OR operation in between the specified keywords. For example, the phrase is “I like Mango”, then phase search looks for all the documents that contain keywords either I, like, or Mango. And if you want to perform an exact phrase search then wrap the phase in between escaped double quotes(\”).

Syntax:

For Phrase search:

db.collectionName.find({$text:{$search:”Phrase”}})

For exact phrase search:

db.collectionName.find({$text:{$search:”\”Phrase”\”}})

Examples:

In the following examples, we are working with:

Database: gfg

Collection: content

Document: The content collection contains the three documents.

  • Find the phrase:
db.content.find({$text:{$search:"I love dogs"}})

Here, we search for the phrase “I love dogs”. So, in the result, we get all the documents that contain keywords I, love, or dogs.

  • Find the exact phrase:
db.content.find({$text:{$search:"\"I love dogs\""}})

Here, we search for the exact phrase by wrapping the phase in between escaped double quotes(\”).i.e., “\”I love dogs\””. So, in the result, we only get those documents that contain the specified exact phrase.

How to exclude a term from the search?

Exclusion of term means when you perform search operation and does not want to display the document that contains the specified term, so you can exclude the term by prefixing a search keyword with the minus sign(-). Using minus sign(-) you can exclude all the documents that contain exclude term. For example, you want to display all the documents that contain the keyword “Car” but not the “Cycle” keyword so you can use the following query:

dn.collectionName.find({$text:{$search:"Car -Cycle"}})

For Example:

In the following example, we are working with:

Database: gfg

Collection: content

Document: The content collection contains the three documents.

db.content.find({$text:{$search:"dog -cow"}})

Here, we only display those documents that contain dog not cow.

Search text using aggregation pipeline

We can search text using the aggregation pipeline with the help of the $text query operator in the $match stage. But there are some restrictions for using the $text operator:

  • The first stage in the pipeline must be the $match stage that contains the $text operator.
  • Only once in the stage will a text operator occur.
  • The expression of the text operator cannot appear in expressions of $or or $not.
  • By default, the text search does not return matching documents in the order of the matched scores. If you want to sort in descending score, then use the $meta aggregation expression in the $sort stage.

Note: Text score is a score that is assigned to each document that holds the search term in the index field by the $text operator. The score reflects a document’s importance to a given text search query.

Example:

In the following example, we are working with:

Database: gfg

Collection: people

Document: The people collection contains the five documents.

  • Count the number of the document in which pet value is a cat:
db.people.aggregate([{$match:{$text:{$search:"Cat"}}},
             {$group:{_id:null,total:{$sum:1}}}])

  • Count the number of the document in which pet value is a dog:
db.people.aggregate([{$match:{$text:{$search:"Dog"}}},
             {$group:{_id:null,total:{$sum:1}}}])

  • Return the Sorted result by using text score:
db.people.aggregate([{$match:{$text:{$search:"Dog"}}},
                     {$sort:{score:{$meta:"textScore"}}},
                     {$project:{_id:0,name:1}}])

Here, we return the result(all the documents that contain the specified text,i.e., “Dog”) in the sorted form using textScore.



Last Updated : 08 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads