Docs Menu

Docs HomeMongoDB Manual

Create a Text Index

On this page

  • About this Task
  • Before You Begin
  • Procedures
  • Create a Single-Field Text Index
  • Create a Compound Text Index

Note

This page describes text search capabilities for self-managed (non-Atlas) deployments. For data hosted on MongoDB Atlas, MongoDB offers an improved full-text search solution, Atlas Search.

Text indexes support text search queries on fields containing string content. Text indexes improve performance when searching for specific words or phrases 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.

    Atlas Search (available in MongoDB Atlas) supports multiple full-text search indexes on a single collection. To learn more, see the Atlas 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 search 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 search 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 search 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' ]
}
]
←  Text IndexesCreate a Wildcard Text Index →