MongoDB is a NoSQL database that helps with various functions to create a powerful application. When working with larger databases, searching for text across large datasets becomes challenging and inefficient. That’s why we use MongoDB’s text search function that transforms queries into text. Text search in MongoDB is designed for relevance-based matching, enabling users to find the most meaningful results quickly, ideal for search bars, product catalogues, and document search features. In this article, we will learn about the text search and its functionality and performance.
Table of Contents:
What is MongoDB Text Search?
Text search is a function that is used to search for strings in a document that has text in it. This text search helps retrieve all related documents containing the given string, even if it doesn’t match all the strings given. It will give the result even if one word in the string matches in the document.
For example, if we are searching for the string “Book,” the text string will fetch all the matches that relate to Book as well as the results that match, like “The Book Thief,” as The Book Thief contains the word ‘book’, and the text search identifies it as a match.
Why Do We Need To Use Text Search in MongoDB?
Traditional querying or searching methods are not reliable and are mostly time-consuming, and they can’t find data effectively if it is in large amounts. So, using text search, if a person wants to search “Data Science,” the text string won’t consider its case or words around it. If any word is related to data science, the text search will find it and return it as a result. It can even be used when you want to search for comments in multiple fields for a certain topic; The text search will search and give results. It is more flexible than regex functions or any exact match-finding operations.
Unlock the Power of MongoDB Text Search
Boost Your NoSQL Query Precision!
How to Set Up MongoDB Text Search?
Before doing the text search in a document, you need to create a text index for the fields in the document you want to search. Setting up a text index is the first step to using text search in documents.
What is a text index?
A text index in MongoDB is used to increase the querying process. A text index stores references to keywords in string fields, which allows you to perform text searches effectively. If there is no Text index, then MongoDB will scan the entire document to find the particular text search, which will reduce the query performance. A text index allows MongoDB to efficiently search for words within string content. For example, if a document contains the word ‘books’, a search for ‘book’ can still return it based on keyword relevance. You can create a text index on one or multiple fields depending on your search requirements.
Syntax:
db.collection.createIndex({ field1: "text", field2: "text" });
Example:
db.blogs.createIndex({ title: "text", FAQ: "text" });
Explanation: This will create an index on the title and FAQ field in the document or blog collection.
Limitations of Text Index
- Each collection can have only one text index, but that index can cover multiple fields.
- Adding the text string may increase the size of the database, which will reduce the write performance of the database.
- Only strings or arrays of string values in indexed fields are considered during text search.
- Text indexes do not support sorting on fields other than the relevance score (textScore) without using a compound index.
To Search a Single Text in a Collection
Once the text index is set, you can perform the text search using the $text operator.
db = client['mydatabase']
Intellipaat = db['Intellipaat']
Intellipaat.insert_many([
{
"title": "Wings of Fire",
"description": "It is a book about Dr. A.P.J. Kalam."
},
{
"title": "The Book Thief",
"description": "A story of a young girl and her love for books."
},
{
"title": "Introduction to Finance",
"description": "An article published by Intellipaat."
}
])
Intellipaat.create_index([('title', 'text'), ('description', 'text')])
query = { "$text": { "$search": "Book" } }
print("Documents matching 'Book':n")
for doc in Intellipaat.find(query):
print(doc)
Output:
Explanation: Here, all the text search results will be under the { }, and each will have its own index as _id as the text index.
Get 100% Hike!
Master Most in Demand Skills Now!
To Search an Array of Strings in a Collection
db = client['mydatabase']
Intellipaat = db['Intellipaat']
Intellipaat.drop()
Intellipaat.insert_many([
{
"title": "Full-Text Search in Databases",
"content": "Databases use text indexes to perform search operations."
},
{
"title": "Text and Search Basics",
"content": "Basic concepts of text search functionalities."
},
{
"title": "Learn to Cook Easily",
"content": "Easy way of Learning Cooking."
}
])
Intellipaat.create_index([('title', 'text'), ('content', 'text')])
query = {
"$text": { "$search": ""text search"" }
}
results = Intellipaat.find(query)
for doc in results:
print(doc)
Output:
Explanation: Here, the result appeared as Text and Search Basics and Full-Text Search in Database, because those documents contain the exact phrase ‘text search‘.
Removing or Excluding Strings While Text Searching
Sometimes, when you want to exclude some words, it’s like mentioning one word to return but not the other.
For example, “Python -SQL” will search for the word Python but exclude the word SQL.
db = client['mydatabase']
Intellipaat = db['Intellipaat']
Intellipaat.insert_many([
{ "title": "Learn Python", "content": "Learn Python Easily." },
{ "title": "History of SQL and Python", "content": "History and integration of SQL and Python." },
{ "title": "Python for Data Science", "content": "Python is used in machine learning." },
{ "title": "Basics of SQL", "content": "Learn database management." },
])
Intellipaat.create_index([('title', 'text'), ('content', 'text')])
query = { "$text": { "$search": "Python -SQL" } }
results = Intellipaat.find(query)
for Intellipaat in results:
print(Intellipaat)
Output:
Explanation: Here, the $search found the output for Python and eliminated the output that has SQL in it.
Searching Text With Query Operators
These query operators in NoSQL are queries that are used to fetch the text string based on a condition, like the “WHERE” or “IN” operator in SQL. There are some query operators like $gt, $lt, and $in that are filters that are used to search the text string with conditions.
Example:
from datetime import datetime
from pymongo import MongoClient
db = client['mydatabase']
articles = db['articles']
articles.drop()
articles.insert_many([
{
"title": "Introduction to MongoDB",
"content": "MongoDB is a NoSQL database.",
"publishedDate": datetime(2023, 7, 19)
},
{
"title": "Advanced MongoDB Queries",
"content": "Learn about queries in MongoDB.",
"publishedDate": datetime(2023, 6, 24)
},
{
"title": "Basics of SQL Databases",
"content": "Learn SQL database.",
"publishedDate": datetime(2022, 2, 27)
},
{
"title": "MongoDB or SQL",
"content": "Difference between MongoDB and SQL databases.",
"publishedDate": datetime(2022, 1, 3)
},
{
"title": "Learn Web Development",
"content": "All-Rounder for web development.",
"publishedDate": datetime(2023, 2, 1)
}
])
articles.create_index([('title', 'text'), ('content', 'text')])
query = {
"$text": {"$search": "MongoDB"},
"publishedDate": {"$gt": datetime(2023, 1, 1)}
}
results = articles.find(query)
for doc in results:
print(doc)
Output:
Explanation: Here, the $gt fetched only the document that has a publish date in the year 2023.
- The size of each index in the database requires more space in the database, which may affect the performance and storage.
- Text search will perform slower than exact matching queries, as the text search will result in all the relevant searches in the database.
- Text indexes are less efficient with high-cardinality fields (many unique words). Avoid indexing fields with highly variable vocabulary unless needed.
- MongoDB’s default text index ignores common stop words (e.g., “the”, “and”). It also applies stemming, which may match variations (e.g., “run”, “running”).
Best Practices
- Always choose which field you want to search the text; this will increase the performance.
- Try to limit the number of search results; this will reduce the memory usage.
- Text searches are always case-insensitive.
- MongoDB text search always ignores the common words when searching, like “the, is, or, from, that, etc.
Start Building Smarter Apps
Learn MongoDB Text Search for Free!
Conclusion
Text Search is one of the essential tools in MongoDB that helps to search the text string without any need to install search engines or integration tools. By learning to use text search, you can effectively apply search conditions to filter results based on your specific criteria. By understanding the basic structure and performance, you can easily search for any text string efficiently. This feature is especially useful in applications like blogs, e-commerce platforms, and content management systems where text-based queries are frequent.
To learn more about SQL functions, check out this MongoDB and also explore MongoDB Interview Questions prepared by industry experts.
MongoDB Text Search – FAQs
Q1. How to search for text in MongoDB?
To search text in MongoDB, create a text index on the desired fields and use the $text operator with the $search keyword to find matching documents.
Q2. How do I create a text search index in MongoDB?
Use db.collection.createIndex({ field: “text” }) to create a text index in MongoDB.
Q3. How to use $text in MongoDB?
Query with { $text: { $search: “your search string” } } to find matching documents in MongoDB.
Q4. What is text-based search?
Text-based search retrieves documents based on matching keywords or phrases in text fields.
Q5. Is MongoDB good for full-text search?
MongoDB is good for basic full-text search, but for advanced needs, tools like Elasticsearch are better.