Skip to content

Commit ab76d8f

Browse files
authored
DOCS-15083 clarifies how sparse/non-sparse indexes affect exists queries (#1016)
* clarifies how sparse/non-sparse indexes affect exists queries * combine into one paragraph * review feedback * external review
1 parent 1a18583 commit ab76d8f

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

source/reference/operator/query/exists.txt

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,30 @@ The results consist of those documents that do not contain the field
116116
Use a Sparse Index to Improve ``$exists`` Performance
117117
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118118

119-
The following scenario is not optimal because all of the collection's
120-
documents are examined:
121-
122-
- You use a query to retrieve or count documents, and
123-
- use ``field: { $exists: true }``, and
124-
- the ``field`` has a non-:ref:`sparse index <index-type-sparse>` or
125-
does not have an index.
126-
127-
To improve performance, create a :ref:`sparse index <index-type-sparse>`
128-
on the ``field`` as shown in the following scenario:
119+
The following table compares ``$exists`` query performance using sparse
120+
and non-sparse indexes:
121+
122+
.. list-table::
123+
:widths: 20,30,30
124+
:header-rows: 1
125+
126+
* - ``$exists`` Query
127+
- Using a Sparse Index
128+
- Using a Non-Sparse Index
129+
* - ``{ $exists: true }``
130+
- Most efficient. MongoDB can make an exact match and does not
131+
require a ``FETCH``.
132+
- More efficient than queries without an index, but still requires
133+
a ``FETCH``.
134+
* - ``{ $exists: false }``
135+
- Cannot use the index and requires a ``COLLSCAN``.
136+
- Requires a ``FETCH``.
137+
138+
Queries that use ``{ $exists: true }`` on fields that use a non-sparse
139+
index or that use ``{ $exists: true }`` on fields that are not indexed
140+
examine all documents in a collection. To improve performance, create
141+
a :ref:`sparse index <index-type-sparse>` on the ``field`` as shown in
142+
the following scenario:
129143

130144
#. Create a ``stockSales`` collection:
131145

0 commit comments

Comments
 (0)