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 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 Text Search

MongoDB Text Search

Introduction to MongoDB Text Search

MongoDB text search is used to search query operations which were used to perform a text search of a string content, to perform text search on the string the MongoDB will perform text search uses text index and $text operator. The view may not be supported in text search but MongoDB collection can support the text search index.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Below is the syntax.

  1. db.collection_name.find ({ $text: search: “String which was used to search using text search”}})
  2. { $text: {$search: <string>,
    $language: <string>,
    $caseSensitive: <boolean>,
    $diacriticSensitive: <boolean>
    }}

Below is the parameter description syntax of text search.

  • Collection name – Collection name is used to search the document using text search. While searching for data using text search we need to define the collection name in MongoDB.
  • $text –It is a text search operator in MongoDB. The text operator will perform a text search on the content of the fields.
  • Search – Search is used to search a specific string using text search. A string in MongoDB passes and uses the query.
  • Language – It is an optional parameter in the text search. The language in text search determines the list of stop words.
  • Case sensitive & Diacritic sensitive – This is a Boolean flag that was used to enable or disable the case sensitive search. It is an optional parameter in the text search.

How to Enable Text Search in MongoDB?

  • Below is the procedure to enable a text search.
  • To enable text search we need to create a text index on our collection. A collection contains only have one text search index in MongoDB, but the text index on the collection covers multiple fields.
  • Text operators will be used to tokenize the search string which was used in whitespace and punctuation as delimiters.
  • Text operators will be used to perform logical OR and all such tokens in the search string in MongoDB.
  • We can also use the search to extract phrases in MongoDB by wrapping them into double quotes.
  • If the $search phrase will contain phrases and individual terms text search will match the documents which include the phrase.
  • We can also exclude the character in the text search. To exclude the word we need to prepend character in text search.

How to Create a Text index in MongoDB?

  • Below is an example to create a text index.
  • In the below example we have created an index on mongo_text collections. We have created the text index name as name_text_description_text.
  • we have used two fields to create a text search index, the first field contains the name of the text search index and the second field contains the description of an index.

db.mongo_text.createIndex( { name: "text", description: "text" } )

MongoDB Text Search output 1

  • We can also create a collection at the time of creating a text search index. The below example shows that create collection and index in one query.

use mongo_test
show collections
db.mongo_text_col.createIndex( { name: "text", description: "text" } )

MongoDB Text Search output 2

  • In the above example, we have created an index and collection in one query. After creating index and collection one query create a collection automatically flag displayed true.
  • Also, the number of the index before the index creation flag display one and the number of the index after the flag will be displayed as two.
  • After the successful execution of the query, the flag ok will display one.

How to delete the Text index?

  • Below is the procedure to delete the text index.

db.mongo_text.dropIndex("name_text_description_text")

MongoDB Text Search output 3

  • In the above example, we have deleted one index. In mongo_text collection, there are two indexes is present, one is on id filed and the second index is text index.
  • We have deleted the text index from the mongo_text collection using the drop index method.

How to use the Text index?

  • The below example shows the use of the text index.

db.mongo_text.find( { $text: { $search: "MongoDB" } } )

output 4

  • In the above example, we have a search name as MongoDB from all the documents from mongo_text collections.
  • After using the text index on mongo_text collection it will display all the collections which contain the MongoDB word.

Recommended Articles

This is a guide to MongoDB Text Search. Here we discuss How to enable, create, delete, and use the Text Search in MongoDB along with the syntax and parameters. You may also look at the following articles to learn more –

  1. MongoDB Collection
  2. NOT in MySQL
  3. LIKE in MySQL
  4. MongoDB GUI Tools
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

*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