Skip to content

Commit 0087588

Browse files
author
Dave Cuthbert
authored
DOCSP-30780 wildcard compound index page (#3389)
* DOCSP-30780 wildcard indexes * Staging * Staging * Review feedback * Review feedback * Staging * Staging * Review feedback * Force build
1 parent 6f4af48 commit 0087588

14 files changed

+499
-236
lines changed

source/core/index-wildcard.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ You can:
3131
- :ref:`Create a wildcard index on a single field
3232
<ex-wildcard-single-field>`
3333
- :ref:`Create a wildcard index on all fields <ex-wildcard-all-fields>`
34+
- :ref:`Create a compound wildcard index <wildcard-index-compound>`
3435
- :ref:`Query and sort with wildcard indexes
3536
<wildcard-index-query-sort-support>`
3637

@@ -225,8 +226,6 @@ Restrictions
225226
For more information on shard key selection, see
226227
:ref:`sharding-shard-key`.
227228

228-
- You cannot create a :ref:`compound <index-type-compound>` index.
229-
230229
- You cannot specify the following properties for a wildcard index:
231230

232231
- :ref:`Time to Live (TTL) <index-feature-ttl>`
@@ -238,13 +237,15 @@ Restrictions
238237
- :ref:`2dsphere (Geospatial) indexes <2dsphere-index>`
239238
- :ref:`Hashed indexes <index-type-hashed>`
240239

241-
.. important::
242-
243-
.. include:: /includes/indexes/wildcard-index-note-text.rst
240+
.. include:: includes/indexes/wildcard-restrictions-compound.rst
244241

245242
For complete documentation on wildcard index creation restrictions, see
246243
:ref:`Wildcard Index Restrictions <wildcard-index-restrictions-create>`.
247244

245+
.. important::
246+
247+
.. include:: /includes/indexes/wildcard-index-note-text.rst
248+
248249
Additional Considerations
249250
~~~~~~~~~~~~~~~~~~~~~~~~~
250251

@@ -263,6 +264,7 @@ Learn More
263264
:hidden:
264265

265266
/reference/indexes/index-wildcard-single
267+
/reference/indexes/index-wildcard-compound
266268
/reference/indexes/index-wildcard-query-and-sort
267269
/reference/indexes/index-wildcard-restrictions
268270

source/includes/extracts-wildcard-indexes.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ content: |
9090
}
9191
}
9292
93-
With the exception of explicitly including ``_id`` field, you cannot
94-
combine inclusion and exclusion statements in the
95-
``wildcardProjection`` document.
93+
All of the statements in the ``wildcardProjection`` document must be
94+
either inclusion or exclusion statements. You can also include the
95+
``_id`` field with exclusion statements. This is the only exception to
96+
the rule.
9697
---
9798
ref: wildcard-index-inclusion-exclusion
9899
content: |

source/includes/indexes/index-creation-methods.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ To create wildcard indexes, use a standard index creation command:
22

33
- :dbcommand:`createIndexes`
44
- :method:`~db.collection.createIndex()`
5-
- :method:`~db.collection.createIndexes()`
5+
- :method:`~db.collection.createIndexes()`
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
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-
51
- 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 }``).
2+
``_id`` field in a wildcard index, you must explicitly include it in
3+
the ``wildcardProjection`` document.
4+
5+
.. code-block:: javascript
6+
7+
db.salesData.createIndex(
8+
{ "$**" : 1 },
9+
{ "wildcardProjection" :
10+
{ "_id": 1, "customers.lastName": 1, "customers.FirstName": 1, }
11+
}
12+
)
813
9-
- You can create multiple wildcard indexes in a collection.
14+
- You can create more than one wildcard index on a collection.
1015

1116
- A wildcard index may cover the same fields as other indexes in the
1217
collection.
1318

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.
19+
- Wildcard indexes are :ref:`sparse <index-type-sparse>`. They only
20+
include entries for documents that contain the indexed field.
1721

22+
The document is not indexed if all of the fields in the compound
23+
wildcard index are missing.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
``wildcardProjection`` works with specifications like:
3+
4+
.. code-block:: javascript
5+
:copyable: false
6+
7+
{ "$**": 1 }
8+
{ "userID":, "$**": 1 }
9+
10+
However, you can't define an index that includes the same field in the
11+
wildcard fields and the regular (non-wildcard) fields. To define the
12+
index correctly, use a ``wildcardProjection`` to exclude duplicated
13+
fields from the wildcard pattern.
14+
15+
``wildcardProjection`` does not work with a specification like:
16+
17+
.. code-block:: javascript
18+
:copyable: false
19+
20+
``{ "path.to.field.$**" : 1 }``
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
- A :ref:`compound wildcard index <wildcard-index-compound>` can only
2+
have one wildcard term.
3+
4+
This is an illegal index specification:
5+
6+
.. code-block:: javascript
7+
:copyable: false
8+
9+
{ userID: 1, “someThings.$**: 1, “otherThings.$**: 1 }
10+
11+
12+
- The non-wildcard terms in a ``compound wildcard index`` must be single
13+
key terms. :ref:`Multi-key <index-type-multikey>` index terms are not
14+
permitted.
15+
16+
- The ``wildcardProjection`` option is only valid when the wildcard
17+
field is ``$**``. You cannot use ``wildcardProjection`` when you
18+
specify a field path for the wildcard index term.
19+
20+
This is a valid definition:
21+
22+
.. code-block:: javascript
23+
24+
{
25+
key: { "$**": 1 },
26+
name: "index_all_with_projection",
27+
wildcardProjection: {
28+
"someFields.name": 1,
29+
"otherFields.values": 1
30+
}
31+
}
32+
33+
This is an invalid definition:
34+
35+
.. code-block:: javascript
36+
:copyable: false
37+
38+
{
39+
key: { "someFields.$**": 1 },
40+
name: "invalid_index",
41+
wildcardProjection: {
42+
"someFields.name": 1,
43+
"otherFields.values": 1
44+
}
45+
}
46+
47+
- The ``_id`` field is omitted by default. If you need the ``_id``
48+
field:
49+
50+
- Specify a wildcard index as ``$**``
51+
- Use a ``wildcardProjection``
52+
- Specify the ``_id`` field
53+
54+
55+
.. code-block:: javascript
56+
57+
db.studentGrades.createIndex(
58+
{
59+
"$**": 1,
60+
},
61+
{
62+
wildcardProjection: {
63+
_id: 1,
64+
exams: 1,
65+
extraCredit: 1
66+
}
67+
}
68+
)
69+
70+
- The same field cannot be included in the wildcard fields and the
71+
regular fields. You can use a ``wildcardProjection`` to exclude fields
72+
from the wildcard pattern.
73+
74+
.. code-block:: javascript
75+
76+
db.studentGrades.createIndex(
77+
{
78+
exams: 1,
79+
"$**": 1,
80+
homeworks: 1
81+
},
82+
{
83+
wildcardProjection: {
84+
exams: 0,
85+
homeworks: 0
86+
}
87+
}
88+
)
89+
90+
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
2-
Wildcard indexes do not support the following index types or index
3-
properties:
1+
Wildcard indexes do not support:
42

53
- :ref:`2d (Geospatial) indexes <2d-index-internals>`
64
- :ref:`2dsphere (Geospatial) indexes <2dsphere-index>`
7-
- :ref:`Compound indexes <index-type-compound>`
85
- :ref:`Hashed indexes <index-type-hashed>`
96
- :ref:`Time to Live (TTL) indexes <index-feature-ttl>`
107
- :ref:`Text indexes <index-feature-text>`
118
- :ref:`Unique indexes <index-type-unique>`
129

13-
Wildcard indexes are :ref:`sparse <index-type-sparse>`; they don't
14-
index empty fields. Therefore, wildcard indexes cannot support
15-
querying for documents where a field is ``null`` or does not exist.
10+
Wildcard indexes are :ref:`sparse <index-type-sparse>` indexes. They do
11+
not support queries when an indexed field does not exist. A wildcard
12+
index will index the document if the wildcard field has a ``null``
13+
value.
14+
15+
Starting in MongoDB 7.0, wildcard indexes support ascending (``1``) and
16+
descending (``-1``) sort order. Earlier versions only supported
17+
ascending order.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
MongoDB supports several different index types, including:
3+
4+
- :ref:`text <index-feature-text>`
5+
- :ref:`geospatial <index-feature-geospatial>`
6+
- :ref:`hashed indexes <index-type-hashed>`
7+
8+
See :ref:`index types <index-types>` for more information.
9+
10+
:ref:`Wildcard indexes <wildcard-index-core>` support workloads where
11+
users query against custom fields or a large variety of fields in a
12+
collection:
13+
14+
- You can create a wildcard index on a specific field and its
15+
subpaths or on all of the fields in a document.
16+
17+
For details see, :ref:`wildcard-index-core`.

source/reference/command/createIndexes.txt

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -171,50 +171,8 @@ Each document in the ``indexes`` array can take the following fields:
171171
type <index-types>`. If specifying direction, specify ``1`` for
172172
ascending or ``-1`` for descending.
173173

174-
175-
MongoDB supports several different index types including
176-
:ref:`text <index-feature-text>`, :ref:`geospatial
177-
<index-feature-geospatial>`, and :ref:`hashed
178-
indexes <index-type-hashed>`. See :ref:`index types
179-
<index-types>` for more information.
180-
181-
:ref:`Wildcard indexes <wildcard-index-core>`
182-
support workloads where users query against custom fields or a
183-
large variety of fields in a collection:
184-
185-
- To create a wildcard index on all fields and subfields in a
186-
document, specify ``{ "$**" : 1 }`` as the index key. You
187-
cannot specify a descending index key when creating a wildcard index.
188-
189-
You can also either include *or* exclude specific fields and
190-
their subfields from the index using the optional
191-
``wildcardProjection`` parameter.
192-
193-
.. include:: /includes/extracts/wildcard-index-id.rst
194-
195-
- You can create a wildcard index on a specific field
196-
and its subpaths by specifying the full path to that field as
197-
the index key and append ``"$**"`` to the path:
198-
199-
``{ "path.to.field.$**" : 1 }``
200-
201-
You cannot specify a descending index key when creating a
202-
wildcard index.
203-
204-
The path-specific wildcard index syntax is incompatible with
205-
the ``wildcardProjection`` option. You cannot specify
206-
additional inclusions or exclusions on the specified path.
207-
208-
The wildcard index key **must** use one of the syntaxes listed
209-
above. For example, you cannot specify a
210-
:doc:`compound index key </core/index-compound>`. For more
211-
complete documentation on wildcard indexes, including
212-
restrictions on their creation, see :ref:`wildcard-index-restrictions`.
213-
214-
For examples of wildcard index creation, see
215-
:ref:`createIndexes-command-wildcard-examples`.
216-
217-
174+
..include:: /includes/indexes/wildcard-use-wc-methods.rst
175+
218176
* - ``name``
219177

220178
- string

0 commit comments

Comments
 (0)