- Reference >
- Operators >
- Query and Projection Operators >
- Element Query Operators >
- $exists
$exists¶
On this page
Definition¶
-
$exists¶ Syntax:
{ field: { $exists: <boolean> } }When
<boolean>is true,$existsmatches 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.MongoDB $exists does not correspond to SQL operator
exists. For SQLexists, refer to the$inoperator.
See also
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: