Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

Create a Text Index on Self-Managed Deployments

Note

MongoDB offers an improved full-text search solution, MongoDB Search, and vector search solution, MongoDB Vector Search. We recommend using MongoDB Search indexes or MongoDB Vector Search indexes instead of text indexes.

Text indexes support $text queries on fields containing string content. Text indexes improve performance when searching for specific words or multi-word strings within string content.

To create a text index, use the db.collection.createIndex() method. To index a field that contains a string or an array of string elements, specify the string "text" as the index key:

db.<collection>.createIndex(
{
<field1>: "text",
<field2>: "text",
...
}
)
  • A collection can have at most one text index.

    MongoDB Search (available in MongoDB) supports multiple full-text search indexes on a single collection. To learn more, see the MongoDB Search documentation.

  • You can index multiple fields in a single text index. A text index can contain up to 32 fields. To see an example, see Create a Compound Text Index.

Create a blog collection with the following documents:

db.blog.insertMany( [
{
_id: 1,
content: "This morning I had a cup of coffee.",
about: "beverage",
keywords: [ "coffee" ]
},
{
_id: 2,
content: "Who likes chocolate ice cream for dessert?",
about: "food",
keywords: [ "poll" ]
},
{
_id: 3,
content: "My favorite flavors are strawberry and coffee",
about: "ice cream",
keywords: [ "food", "dessert" ]
}
] )

The following examples show you how to:

Create a text index on the content field:

db.blog.createIndex( { "content": "text" } )

The index supports $text queries on the content field. For example, the following query returns documents where the content field contains the string coffee:

db.blog.find(
{
$text: { $search: "coffee" }
}
)

Output:

[
{
_id: 1,
content: 'This morning I had a cup of coffee.',
about: 'beverage',
keywords: [ 'coffee' ]
},
{
_id: 3,
content: 'My favorite flavors are strawberry and coffee',
about: 'ice cream',
keywords: [ 'food', 'dessert' ]
}
]

The { "content": "text" } index only includes the content field, and does not return matches on non-indexed fields. For example, the following query searches the blog collection for the string food:

db.blog.find(
{
$text: { $search: "food" }
}
)

The preceding query returns no documents. Although the string food appears in documents _id: 2 and _id: 3, it appears in the about and keywords fields respectively. The about and keywords fields are not included in the text index, and therefore do not affect $text query results.

Note

Before you can create the index in this example, you must drop any existing text indexes on the blog collection.

Create a compound text index on the about and keywords fields in the blog collection:

db.blog.createIndex(
{
"about": "text",
"keywords": "text"
}
)

The index supports $text queries on the about and keywords fields. For example, the following query returns documents where the string food appears in either the about or keywords field:

db.blog.find(
{
$text: { $search: "food" }
}
)

Output:

[
{
_id: 3,
content: 'My favorite flavors are strawberry and coffee',
about: 'ice cream',
keywords: [ 'food', 'dessert' ]
},
{
_id: 2,
content: 'Who likes chocolate ice cream for dessert?',
about: 'food',
keywords: [ 'poll' ]
}
]

Back

Text Indexes

On this page