EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Full Text Search
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB JSON
    • MongoDB Careers
    • MongoDB Migration
    • MongoDB Export to Excel
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
    • MongoDB schema
    • MongoDB find by id
    • Mongodb max document size
    • Mongodb not equal
    • MongoDB keyfile
    • MongoDB Hosting
    • MongoDB Kafka Connector
    • MongoDB exists
    • MongoDB express
    • MongoDB Performance
    • MongoDB Connection String
    • MongoDB BI Connector
    • Mongodb aggregation pipeline
    • Mongodb backup and restore
    • MongoDB Drop Collection
    • MongoDB Text Search
    • MongoDB URI
    • MongoDB Full Text Search
    • MongoDB List Databases
    • MongoDB Use Cases
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

MongoDB Full Text Search

MongoDB Full Text Search

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

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

MongoDB Full Text Search 1

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:

student

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:

index over the Name and subject field

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:

MongoDB uri 7

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 –

  1. MongoDB Join
  2. MongoDB findOne()
  3. MongoDB Date Query
  4. MongoDB Database
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (17 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more