Skip to content

Commit d07844e

Browse files
Dave Cuthbertmdb-ashley
andauthored
DOCS-10322 BACKPORT (#2565)
Co-authored-by: Ashley Brown <[email protected]>
1 parent a38fdc1 commit d07844e

File tree

5 files changed

+65
-31
lines changed

5 files changed

+65
-31
lines changed

source/core/index-compound.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ Index Intersection
207207

208208
.. include:: /includes/fact-index-intersection-vs-compound-indexes.rst
209209

210+
Sparse Compound Indexes
211+
-----------------------
212+
213+
.. include:: /includes/indexes/sparse-compound-indexes.rst
214+
215+
210216
Additional Considerations
211217
-------------------------
212218

source/core/index-sparse.txt

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ field.
3131
Create a Sparse Index
3232
---------------------
3333

34-
To create a ``sparse`` index, use the
35-
:method:`db.collection.createIndex()` method with the ``sparse`` option
36-
set to ``true``. For example, the following operation in
37-
:binary:`~bin.mongosh` creates a sparse index on the ``xmpp_id`` field
38-
of the ``addresses`` collection:
34+
To create a sparse index, use the :method:`db.collection.createIndex()`
35+
method with the ``sparse`` option set to ``true``.
36+
37+
For example, the following operation in :binary:`~bin.mongosh` creates a
38+
sparse index on the ``xmpp_id`` field of the ``addresses`` collection:
3939

4040
.. code-block:: javascript
4141

@@ -55,8 +55,8 @@ field.
5555
Behavior
5656
--------
5757

58-
``sparse`` Index and Incomplete Results
59-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58+
Sparse Index and Incomplete Results
59+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6060

6161
If a sparse index would result in an incomplete result set for queries
6262
and sort operations, MongoDB will not use that index unless a
@@ -69,39 +69,27 @@ behavior.
6969

7070
.. include:: /includes/fact-sparse-index-hint-count.rst
7171

72-
Indexes that are ``sparse`` by Default
73-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72+
Indexes that are Sparse by Default
73+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7474

75-
:ref:`2dsphere (version 2) <2dsphere-v2>`, :doc:`2d </core/2d>`,
76-
:doc:`geoHaystack </core/geohaystack>`, and :doc:`text
77-
</core/index-text>` indexes are always ``sparse``.
75+
:ref:`2dsphere (version 2) <2dsphere-v2>`, :ref:`2d <2d-index>`,
76+
:ref:`geoHaystack <index-geohaystack-index>`, and :ref:`text
77+
<index-feature-text>` indexes are always sparse.
7878

79-
``sparse`` Compound Indexes
80-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
79+
.. _sparse-compound-indexes:
8180

82-
Sparse :doc:`compound indexes </core/index-compound>` that only contain
83-
ascending/descending index keys will index a document as long as the
84-
document contains at least one of the keys.
85-
86-
For sparse compound indexes that contain a geospatial key (i.e.
87-
:doc:`2dsphere </core/2dsphere>`, :doc:`2d </core/2d>`, or
88-
:doc:`geoHaystack </core/geohaystack>` index keys) along with
89-
ascending/descending index key(s), only the existence of the geospatial
90-
field(s) in a document determine whether the index references the
91-
document.
81+
Sparse Compound Indexes
82+
~~~~~~~~~~~~~~~~~~~~~~~
9283

93-
For sparse compound indexes that contain :doc:`text </core/index-text>`
94-
index keys along with ascending/descending index keys, only the
95-
existence of the ``text`` index field(s) determine whether the index
96-
references a document.
84+
.. include:: /includes/indexes/sparse-compound-indexes.rst
9785

9886
.. _sparse-unique-index:
9987

10088
``sparse`` and ``unique`` Properties
10189
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10290

103-
An index that is both ``sparse`` and :ref:`unique <index-type-unique>`
104-
prevents collection from having documents with duplicate values for a
91+
An index that is both sparse and :ref:`unique <index-type-unique>`
92+
prevents a collection from having documents with duplicate values for a
10593
field but allows multiple documents that omit the key.
10694

10795
Examples
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Compound indexes can contain different types of sparse indexes. The
2+
combination of index types determines how the compound index matches
3+
documents.
4+
5+
This table summarizes the behavior of a compound index that contains
6+
different types of sparse indexes:
7+
8+
.. list-table::
9+
:header-rows: 1
10+
11+
* - Compound Index Components
12+
- Compound Index Behavior
13+
14+
* - | Ascending indexes
15+
| Descending indexes
16+
- Only indexes documents that contain a value for at least one of
17+
the keys.
18+
19+
* - | Ascending indexes
20+
| Descending indexes
21+
| :ref:`Geospatial indexes <index-feature-geospatial>`
22+
- Only indexes a document when it contains a value for one of
23+
the ``geospatial`` fields. Does not index documents in the
24+
ascending or descending indexes.
25+
26+
* - | Ascending indexes
27+
| Descending indexes
28+
| :ref:`Text indexes <index-feature-text>`
29+
- Only indexes a document when it matches one of the ``text``
30+
fields. Does not index documents in the ascending or descending
31+
indexes.
32+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This index is a :ref:`sparse index <index-type-sparse>`. If you want to
2+
use a sparse index to create a :ref:`compound index
3+
<index-type-compound>`, first review the special considerations of using
4+
:ref:`sparse compound indexes <sparse-compound-indexes>`.

source/indexes.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,18 @@ provides two special indexes: :doc:`2d indexes </core/2d>` that uses
173173
planar geometry when returning results and :doc:`2dsphere indexes
174174
</core/2dsphere>` that use spherical geometry to return results.
175175

176-
See :doc:`/core/geospatial-indexes` for a high level introduction to
176+
.. include:: /includes/indexes/sparse-index-considerations.rst
177+
178+
See :ref:`index-feature-geospatial` for a high level introduction to
177179
geospatial indexes.
178180

179181
Text Search Indexes
180182
~~~~~~~~~~~~~~~~~~~
181183

182184
.. include:: /includes/indexes/text-search-indexes-overview.rst
183185

186+
.. include:: /includes/indexes/sparse-index-considerations.rst
187+
184188
Hashed Indexes
185189
~~~~~~~~~~~~~~
186190

0 commit comments

Comments
 (0)