@@ -116,16 +116,30 @@ The results consist of those documents that do not contain the field
116
116
Use a Sparse Index to Improve ``$exists`` Performance
117
117
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118
118
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:
129
143
130
144
#. Create a ``stockSales`` collection:
131
145
0 commit comments