Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Create a Wildcard Text Index

On this page

  • About this Task
  • Before You Begin
  • Procedure
  • Results
  • Search for a Single Word
  • Search for Multiple Terms
  • Search for an Exact Phrase
  • Learn More

You can create a text index that contains every document field with string data in a collection. These text indexes are called wildcard text indexes. Wildcard text indexes support text search on unknown, arbitrary, or dynamically generated fields.

To create a wildcard text index, set the index key to the wildcard specifier ($**) and set the index value to text:

db.<collection>.createIndex( { "$**": "text" } )

Wildcard text indexes are distinct from wildcard indexes. Wildcard text indexes support queries that use the $text operator, while wildcard indexes do not.

After you create a wildcard text index, when you insert or update documents, the index updates to include any new string field values. As a result, wildcard text indexes negatively impact performance for inserts and updates.

Only use wildcard text indexes when the fields you want to index are unknown or may change. Wildcard text indexes don't perform as well as targeted text indexes on specific fields. If your collection contains arbitrary field names that prevent targeted indexes, consider remodeling your schema to have consistent field names. To learn more about targeted indexes, see Create Indexes to Support Your Queries.

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" ]
}
] )

Create a wildcard text index on the blog collection:

db.blog.createIndex( { "$**": "text" } )

The wildcard text index supports text search queries on all fields in the collection. Consider the following queries:

Query the blog collection for 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 preceding query returns all documents that contain the string coffee in any field.

Query the blog collection for documents that contain the string poll or coffee:

db.blog.find( { $text: { $search: "poll 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' ]
},
{
_id: 2,
content: 'Who likes chocolate ice cream for dessert?',
about: 'food',
keywords: [ 'poll' ]
}
]

The preceding query returns documents that contain the string poll or coffee in any field.

Query the blog collection for documents that contain the phrase chocolate ice cream:

db.blog.find( { $text: { $search: "\"chocolate ice cream\"" } } )

Output:

[
{
_id: 2,
content: 'Who likes chocolate ice cream for dessert?',
about: 'food',
keywords: [ 'poll' ]
}
]

The preceding query returns documents that contain the exact phrase chocolate ice cream in any field.

← Create a Text Index