MongoDB - $text Evaluation Operator
Definition
The $text evaluation operator in MongoDB is used to perform text searches on fields that are indexed with a text index. It enables searching for words, phrases, or terms within string content stored in documents.
Steps to Use $text Evaluation Operator in MongoDB
1). Open the MongoDB Shell using mongosh.
2). Select the database using use database_name.
3). Check the existing documents using find().
4). Create a text index on the field you want to search.
5). Use the $text operator in the query condition.
6). Specify the word or phrase to search using $search.
7). Execute the query.
8). View the matching documents returned by MongoDB.
Example
use studentdb
db.students.createIndex(
{
description: "text"
}
)
db.students.find(
{
$text: { $search: "computer" }
}
)
Explanation:
This query returns all documents where the description field contains the word computer.