Text Search: Part 1
Learn and practice commands to create text indexes, and apply text search to a MongoDB collection.
We use the text search operation to search words, phrases, or words with conditions against text data stored in one or more fields. MongoDB provides text search with the help of text index and supports multiple languages. This is also called a full-text search.
First, a text
index is created on one or more fields, and then with help of the $text
operator text search is performed.
We can only use one text
index on a collection.
Note: The fields should have string content.
Create text index
To perform the text search, the field should have a text index on the field.
Text index on one field
Below is the syntax to create a text index on a field of the document.
db.<collection-name>.createIndex({
<field-name>: "text",
...
});
...