- Reference >
- Operators >
- Query and Projection Operators >
- Element Query Operators >
- $exists
$exists¶
On this page
Definition¶
-
$exists
¶ Syntax:
{ field: { $exists: <boolean> } }
When
<boolean>
is true,$exists
matches the documents that contain the field, including documents where the field value isnull
. If<boolean>
is false, the query returns only the documents that do not contain the field. [1]MongoDB
$exists
does not correspond to SQL operatorexists
. For SQLexists
, refer to the$in
operator.
See also
[1] | Starting in MongoDB 4.2, users can no longer use the query filter
$type: 0 as a synonym for
$exists:false . To query for null or missing fields, see
Query for Null or Missing Fields. |
Examples¶
Exists and Not Equal To¶
Consider the following example:
This query will select all documents in the inventory
collection
where the qty
field exists and its value does not equal 5
or
15
.
Null Values¶
The following examples uses a collection named records
with the
following documents:
$exists: true
¶
The following query specifies the query predicate a: { $exists: true }
:
The results consist of those documents that contain the field a
,
including the document whose field a
contains a null value:
$exists: false
¶
The following query specifies the query predicate b: { $exists: false }
:
The results consist of those documents that do not contain the field
b
:
Starting in MongoDB 4.2, users can no longer use the query filter
$type: 0
as a synonym for
$exists:false
. To query for null or missing fields, see
Query for Null or Missing Fields.
Use a Sparse Index to Improve $exists
Performance¶
The following table compares $exists
query performance using sparse
and non-sparse indexes:
$exists Query |
Using a Sparse Index | Using a Non-Sparse Index |
---|---|---|
{ $exists: true } |
Most efficient. MongoDB can make an exact match and does not
require a FETCH . |
More efficient than queries without an index, but still requires
a FETCH . |
{ $exists: false } |
Cannot use the index and requires a COLLSCAN . |
Requires a FETCH . |
Queries that use { $exists: true }
on fields that use a non-sparse
index or that use { $exists: true }
on fields that are not indexed
examine all documents in a collection. To improve performance, create
a sparse index on the field
as shown in
the following scenario:
Create a
stockSales
collection:The document with an
_id
of:3
has a nullauditDate
value.5
is missing theauditDate
value.
Create a sparse index on the
auditDate
field:The following example counts the documents where the
auditDate
field has a value (including null) and uses the sparse index:The example returns 5. The document that is missing the
auditDate
value is not counted.
Tip
If you only need documents where the field
has a non-null value,
you:
- Can use
$ne: null
instead of$exists: true
. - Do not need a sparse index on the
field
.
For example, using the stockSales
collection:
The example returns 4. Documents that are missing the auditDate
value or have a null auditDate
value are not counted.
See also