Updated March 14, 2023
Introduction to MongoDB Full Text Search
MongoDB provides different types of functionality to the user; the full-text search is one of the functionalities that is provided by MongoDB. Normally, we perform the search operation to find the particular content using the find () method, but MongoDB provides the $text operator to perform the full search. In full-text search, we are able to search content as per our requirement on the field index with a text index. We can perform the full-text search by using text index and $text operator as per user requirement as well as we can search the text string from the collection.
Syntax of MongoDB Full Text Search
Given below is the syntax mentioned:
db. Specified collection name.find({$text: {$search: “specified string”}})
Explanation:
In the above syntax, we use the find () method with the different parameters as follows:
- Specified collection name: The specified collection name means the actual collection name that we already created.
- find (): The find is a method and it is used to perform the search operation.
- $text: It is the $text query operator used to perform a search on collection as per our requirement.
- $search: It is the keyword to perform the search operation.
- specified string: The specified string means an actual string that we need to search in the collection as well as we can search the exact phrases by using the search operation.
How to Perform Full Text Search in MongoDB?
Given below shows how full-text search works in MongoDB:
What is a text index?
MongoDB gives text files to help text search inquiries on string content. Text lists can incorporate any field whose worth is a string or a variety of string components. To perform text search questions, you should have a book file on your assortment. An assortment can just have one content inquiry file, yet that record can cover different fields.
How does the $text operator work?
MongoDB gives text lists to help text search inquiries on string content. Text records can incorporate any field whose worth is a string or a variety of string components. To perform text search questions, you should have a book record on your assortment. An assortment can just have one content pursuit record, yet that list can cover different fields.
How can we find the exact phrase in MongoDB?
You can likewise look for definite expressions by enclosing them with twofold statements. In the event that the $search string incorporates an expression and individual terms, text search will just match archives that incorporate the expression.
We see how we can use the $text operator, but at the same, we need to follow some rules and restrictions as follows:
A question can determine, probably, one $text articulation.
- The $text inquiry can not show up in $nor articulations.
- The $text inquiry can not show up in $elemMatch question articulations or $elemMatch projection articulations.
- To utilize a $text question in a $or articulation, all conditions in the $or exhibit should be listed.
- You can’t utilize hint () if the question incorporates a $text inquiry articulation.
- You can’t determine $natural sort request if the inquiry incorporates a $text articulation.
- You can’t join the $text articulation, which requires a unique book file, with an inquiry administrator that requires an alternate kind of uncommon record. For instance, you can’t consolidate $text articulation with the $near administrator.
- Perspectives don’t uphold text search.
When we use $text with aggregation, then we need to maintain rules, or we can say restriction as follows:
- The $match stage that incorporates a $text should be the primary stage ready to go.
- A $text administrator can just happen once in the stage.
- The $text administrator articulation can’t show up in $or or $not articulations.
- The content pursuit, of course, doesn’t return the coordinating with reports arranged by coordinating with scores. To sort by diving score, utilize the $meta conglomeration articulation in the $sort stage.
Example of MongoDB Full Text Search
Given below is the example of MongoDB full text search:
First, we need to create the database by using the following statement as follows.
Code:
use sample
Explanation:
- By using the above statement, we successfully created a sample database.
- The final output of the above statement we illustrated by using the following screenshot as follows.
Output:
Now create the collection by using the following statement as follows.
Code:
db.createCollection(“student”)
Explanation:
- By using the above statement, we created a new collection name as a student, as shown.
- The final output of the above statement we illustrated by using the following screenshot as follows.
Output:
Now we need to insert the document newly created collection by using the following statement as follows.
Code:
{"_id":{"$oid":"60f2dce459b1a9838581ca1d"},"Name":"Rohit","subject":"This is
DBMS subject","result":"pass"}
{"_id": {"$oid": "60f2df3259b1a9838581ca27"}, "Name": "Virat", "subject": "Thi
s cyber
security subject", "result": "pass"}
{"_id": {"$oid": "60f2df5759b1a9838581ca28"},"Name": "Jenny", "subject": "This is
Information Technology subject", "result": "pass"}
{"_id": {"$oid": "60f2df8b59b1a9838581ca2a"},"Name": "Sameer", "subject": "This is
MongoDB subject", "result": "pass"}
Now let’s see how we can perform the full-text search as follows.
First, we need to create the index by using the following statement as follows.
Code:
db.student.createIndex( {Name: "text", subject: "text" } )
Explanation:
- In the above statement, we use an index statement to create the text index.
- Here we created the index over the name and subject field as shown.
- The final output of the above statement we illustrated by using the following screenshot as follows.
Output:
Now we can perform the full text search as follows.
Code:
db.student.find( { $text: { $search: "DBMS" } } )
Explanation:
- In the above example, we try to implement the full-text search, here we use the find () method, and we need to find the above-mentioned string as shown.
- The final output of the above statement we illustrated by using the following screenshot as follows.
Output:
So in this way, we can perform the exact phrase search as per our requirement.
Conclusion
From the above article, we have seen the basic syntax of the full-text search, and we also saw different examples of the full-text search. From this article, we have seen how and when we use MongoDB full-text search.
Recommended Articles
This is a guide to MongoDB Full Text Search. Here we discuss the introduction, how to perform a full text search in MongoDB? and an example. You may also have a look at the following articles to learn more –