Docs Menu
Docs Home
/
MongoDB Manual
/ / / /

Wildcard Index Signature

On this page

  • Projection Signature Display
  • Example
  • Learn More

Starting in MongoDB 5.0, the wildcardProjection option for wildcard indexes is included in the index signature. The index signature is the combination of parameters that uniquely identify the index. This means that you can create multiple wildcard indexes with the same key pattern as long as the wildcardProjection options do not contain the same fields.

Starting in MongoDB 6.3, 6.0.5, and 5.0.16, the wildcardProjection field stores the index projection in its submitted form. Earlier versions of the server may have stored the projection in a normalized form.

The server uses the index the same way, but you may notice a difference in the output of the listIndexes and db.collection.getIndexes() commands.

Consider the following wildcard index on a books collection:

db.books.createIndex(
{
"$**": 1
},
{
wildcardProjection: {
"author.name": 1,
"author.website": 1
},
name: "authorWildcard"
}
)

The index key pattern is "$**". You can create another wildcard index with the same key pattern if you specify a different wildcardProjection. For example:

db.books.createIndex(
{
"$**": 1
},
{
wildcardProjection: {
"publisher.name": 1
},
name: "publisherWildcard"
}
)

To view the created indexes, run the getIndexes() method:

db.books.getIndexes()

Output:

[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{
v: 2,
key: { '$**': 1 },
name: 'authorWildcard',
wildcardProjection: { author: { website: true, name: true }, _id: false }
},
{
v: 2,
key: { '$**': 1 },
name: 'publisherWildcard',
wildcardProjection: { publisher: { name: true }, _id: false }
}
]

Back

Wildcard Indexes on Embedded Objects and Arrays

Next

Wildcard Index Restrictions