Skip to content

Commit e18af7c

Browse files
author
Dave Cuthbert
authored
DOCSP-30639 wildcard index main page (#3374)
* DOCSP-30639 wildcard index main page * Example * Monospace
1 parent e3b16cd commit e18af7c

8 files changed

+474
-359
lines changed

source/core/index-wildcard.txt

Lines changed: 41 additions & 355 deletions
Large diffs are not rendered by default.

source/includes/extracts-wildcard-indexes.yaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ content: |
4747
Administrators want to create indexes to support queries on any
4848
subfield of ``userMetadata``.
4949
50-
A wildcard index on ``userMetadata``
51-
can support single-field queries on ``userMetadata``,
52-
``userMetadata.likes``, ``userMetadata.dislikes``, and
53-
``userMetadata.age``:
50+
A wildcard index on the ``userMetadata`` field
51+
can support single-field queries on:
52+
53+
- ``userMetadata``
54+
- ``userMetadata.likes``
55+
- ``userMetadata.dislikes``
56+
- ``userMetadata.age``
5457
5558
.. code-block:: bash
5659
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
To create wildcard indexes, use a standard index creation command:
2+
3+
- :dbcommand:`createIndexes`
4+
- :method:`~db.collection.createIndex()`
5+
- :method:`~db.collection.createIndexes()`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
- Wildcard indexes can support at most *one* field in any given query
2+
predicate. For more information on wildcard index query
3+
support, see :ref:`wildcard-index-query-sort-support`.
4+
5+
- Wildcard indexes omit the ``_id`` field by default. To include the
6+
``_id`` field in the wildcard index, you must explicitly include it in
7+
the wildcardProjection document (i.e. ``{ "_id" : 1 }``).
8+
9+
- You can create multiple wildcard indexes in a collection.
10+
11+
- A wildcard index may cover the same fields as other indexes in the
12+
collection.
13+
14+
- Wildcard indexes are :ref:`sparse <index-type-sparse>` and only
15+
contain entries for documents that have the indexed field, even if the
16+
index field contains a null value.
17+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Wildcard indexes do not replace workload-based index planning.
2+
3+
For more information on creating indexes that support your workload, see
4+
:ref:`create-indexes-to-support-queries`.
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
.. _wildcard-index-query-sort-support:
2+
3+
=====================================
4+
Wildcard Index Query and Sort Support
5+
=====================================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
MongoDB supports creating wildcard indexes on a field, or a set of
16+
fields, to support different types of queries.
17+
18+
Wildcard indexes support:
19+
20+
- :ref:`Covered Queries <wc-index-covered>`
21+
- :ref:`Multi-Field Query Predicates <wc-index-multi>`
22+
- :ref:`Queries with Sort <wc-index-sort>`
23+
- :ref:`Queries with Explicit Array Indices <wc-index-explicit>`
24+
25+
Some query patterns are unsupported: :ref:`Unsupported Query Patterns
26+
<wc-index-unsupported>`
27+
28+
.. important::
29+
30+
.. include:: /includes/indexes/wildcard-not-planning-replacement.rst
31+
32+
.. _wc-index-covered:
33+
34+
Covered Queries
35+
~~~~~~~~~~~~~~~
36+
37+
Wildcard indexes can support a :ref:`covered query <covered-queries>`
38+
**only if** all of the following are true:
39+
40+
- The query planner selects the wildcard index for satisfying the
41+
query predicate.
42+
43+
- The query predicate specifies *exactly* one field covered by the wildcard
44+
index.
45+
46+
- The projection explicitly excludes ``_id`` and includes *only* the query
47+
field.
48+
49+
- The specified query field is never an array.
50+
51+
Consider the following wildcard index on the ``employees`` collection:
52+
53+
.. code-block:: javascript
54+
55+
db.employees.createIndex( { "$**" : 1 } )
56+
57+
The following operation queries for a single field
58+
``lastName`` and projects out all other fields from the
59+
resulting document:
60+
61+
.. code-block:: javascript
62+
63+
db.employees.find(
64+
{ "lastName" : "Doe" },
65+
{ "_id" : 0, "lastName" : 1 }
66+
)
67+
68+
Assuming that the specified ``lastName`` is never an array, MongoDB
69+
can use the ``$**`` wildcard index for supporting a covered query.
70+
71+
.. _wc-index-multi:
72+
73+
Multi-Field Query Predicates
74+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75+
76+
Wildcard indexes can support at most *one* query predicate field. That
77+
is:
78+
79+
- MongoDB cannot use a non-wildcard index to satisfy one part of a
80+
query predicate and a wildcard index to satisfy another.
81+
82+
- MongoDB cannot use one wildcard index to satisfy one part of a query
83+
predicate and another wildcard index to satisfy another.
84+
85+
- Even if a single wildcard index could support multiple query fields,
86+
MongoDB can use the wildcard index to support only one of the query
87+
fields. All remaining fields are resolved without an index.
88+
89+
However, MongoDB may use the same wildcard index for satisfying each
90+
independent argument of the query :query:`$or` or aggregation
91+
:expression:`$or` operators.
92+
93+
.. _wc-index-sort:
94+
95+
Queries with Sort
96+
~~~~~~~~~~~~~~~~~
97+
98+
MongoDB can use a wildcard index for satisfying the
99+
:method:`~cursor.sort()` **only if** all of the following are true:
100+
101+
- The query planner selects the wildcard index for satisfying the
102+
query predicate.
103+
104+
- The :method:`~cursor.sort()` specifies **only** the query predicate
105+
field.
106+
107+
- The specified field is never an array.
108+
109+
If the above conditions are not met, MongoDB cannot use the wildcard
110+
index for the sort. MongoDB does not support :method:`~cursor.sort`
111+
operations that require a different index from that of the query
112+
predicate. For more information, see :ref:`index-intersection-sort`.
113+
114+
Consider the following wildcard index on the ``products`` collection:
115+
116+
.. code-block:: javascript
117+
118+
db.products.createIndex( { "product_attributes.$**" : 1 } )
119+
120+
The following operation queries for a single field
121+
``product_attributes.price`` and sorts on that same field:
122+
123+
.. code-block:: javascript
124+
125+
db.products.find(
126+
{ "product_attributes.price" : { $gt : 10.00 } },
127+
).sort(
128+
{ "product_attributes.price" : 1 }
129+
)
130+
131+
Assuming that the specified ``price`` is never an array, MongoDB
132+
can use the ``product_attributes.$**`` wildcard index for satisfying
133+
both the :method:`~db.collection.find()` and :method:`~cursor.sort()`.
134+
135+
.. _wc-index-explicit:
136+
.. _wildcard-query-support-explicit-array-indices:
137+
138+
Queries with Explicit Array Indices
139+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
140+
141+
MongoDB wildcard indexes do not record the array position of any given
142+
element in an array during indexing. However, MongoDB may still select
143+
the wildcard index to answer a query which includes a field path with
144+
one or more explicit array indices (for example,
145+
``parentArray.0.nestedArray.0``). Due to the increasing complexity of
146+
defining index bounds for each consecutive nested array, MongoDB does
147+
not consider the wildcard index to answer a given field path in the
148+
query if that path contains more than ``8`` explicit array indices.
149+
MongoDB can still consider the wildcard index to answer other field
150+
paths in the query.
151+
152+
For example:
153+
154+
.. code-block:: json
155+
156+
{
157+
"parentObject" : {
158+
"nestedArray" : [
159+
"elementOne",
160+
{
161+
"deeplyNestedArray" : [ "elementTwo" ]
162+
}
163+
]
164+
}
165+
}
166+
167+
MongoDB can select a wildcard index which includes ``parentObject`` to
168+
satisfy the following queries:
169+
170+
- ``"parentObject.nestedArray.0" : "elementOne"``
171+
- ``"parentObject.nestedArray.1.deeplyNestedArray.0" : "elementTwo"``
172+
173+
If a given field path in the query predicate specifies more than 8
174+
explicit array indices, MongoDB does not consider the wildcard index for
175+
answering that field path. MongoDB instead either selects another
176+
eligible index to answer the query, *or* performs a collection scan.
177+
178+
Note that wildcard indexes themselves do not have any limits on the
179+
depth to which they traverse a document while indexing it; the
180+
limitation only applies to queries which explicitly specify exact array
181+
indices. By issuing the same queries without the explicit array indices,
182+
MongoDB may select the wildcard index to answer the query:
183+
184+
- ``"parentObject.nestedArray" : "elementOne"``
185+
- ``"parentObject.nestedArray.deeplyNestedArray" : "elementTwo"``
186+
187+
.. seealso::
188+
189+
:limit:`Nested Depth for BSON Documents`
190+
191+
.. _wc-index-unsupported:
192+
193+
Unsupported Query Patterns
194+
~~~~~~~~~~~~~~~~~~~~~~~~~~
195+
196+
Wildcard indexes **cannot** support the following query patterns:
197+
198+
- Queries that check if a field does not exist
199+
200+
- Queries that check if a field is or is not equal to a document or an
201+
array
202+
203+
- Queries that check if a field is equal to null
204+
205+
For details, see :ref:`wildcard-index-restrictions-query-aggregation`.
206+

0 commit comments

Comments
 (0)