How to Perform Text Search MongoDB

MongoDB Text Search operation is very important operation :

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:

Query:

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

Output:

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.

Query:

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

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

Output:

MongoDB Text Search

MongoDB Text Search technique finds a piece of text or a specified word from the string fields. It enables users to search for specific words or phrases within text fields efficiently.

The text index and $text operator perform text search in MongoDB, providing a powerful way to search for textual data in MongoDB collections.

Similar Reads

Text index

Text indexes in MongoDB are to find the specified text from the string content. Text indexes should be either a string or an array of string elements....

$text Operator

$text operator in MongoDB is used to search the text index. This operator performs text search operations on the collection with a text index....

How to Perform Text Search MongoDB

MongoDB Text Search operation is very important operation :...

MongoDB Text Search Example

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

Phrase Search in MongoDB

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....

MongoDB Phrase Search Example

Let’s look at some examples of phrase search in MongoDB, to understand this concept better....

How to Exclude a Term From the Text Search?

Exclusion of term means when you perform search operation and do 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(-)....

Exclude a Term From the Text Search Example

Let’s look at some examples of excluding a term from text search in MongoDB to understand the concept better....

MongoDB Text Search In 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:...

Text Search in Aggregation Pipeline Example

In the following example, we are working with:...

Conclusion

In this guide, we have covered various techniques for text search in MongoDB. We have explained methods like text index & text operator to search text and phrases. We also checked text search using aggregation pipelines....

Contact Us