Skip to content

Commit 79d75e4

Browse files
committed
DOCS-827 query for nulls
1 parent c6cffe8 commit 79d75e4

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

source/faq/developers.txt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,68 @@ for additional information.
427427
- The :source:`jsobj.h <src/mongo/db/jsobj.h>` source file for the
428428
definition of ``MinKey`` and ``MaxKey``.
429429

430+
.. _faq-developers-query-for-nulls:
431+
432+
How do I query for nulls?
433+
-------------------------
434+
435+
A document may contain a ``null`` value.
436+
437+
Consider a collection ``test`` that contains the following documents:
438+
439+
.. code-block:: javascript
440+
441+
{ _id: 1, cancelDate: null }
442+
{ _id: 2 }
443+
444+
Different query operators treat ``null`` values differently:
445+
446+
- The ``{ cancelDate : null }`` query matches documents that either
447+
contains the ``cancelDate`` field whose value is ``null`` *or* that
448+
do not contain the ``cancelDate`` field:
449+
450+
.. code-block:: javascript
451+
452+
db.test.find( { cancelDate: null } )
453+
454+
The query returns both documents:
455+
456+
.. code-block:: javascript
457+
458+
- The ``{ cancelDate : { $type: 10 } }`` query matches documents that
459+
contains the ``cancelDate`` field whose value is ``null`` *only*;
460+
i.e. the value of the ``cancelDate`` field is of BSON Type ``Null``
461+
(i.e. ``10``) :
462+
463+
.. code-block:: javascript
464+
465+
db.test.find( { cancelDate : { $type: 10 } } )
466+
467+
The query returns only the document that contains the ``null`` value:
468+
469+
.. code-block:: javascript
470+
471+
{ "_id" : 1, "cancelDate" : null }
472+
473+
- The ``{ cancelDate : { $exists: false } }`` query matches documents
474+
that that do not contain the ``cancelDate`` field:
475+
476+
.. code-block:: javascript
477+
478+
db.test.find( { cancelDate : { $exists: false } } )
479+
480+
The query returns only the document that contains the ``null`` value:
481+
482+
.. code-block:: javascript
483+
484+
{ "_id" : 2 }
485+
486+
.. seealso::
487+
488+
- :operator:`$type`
489+
490+
- :operator:`$exists`
491+
430492
.. _faq-developers:
431493

432494
.. _faq-restrictions-on-collection-names:

source/reference/operator/exists.txt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,26 @@ $exists
1818
.. code-block:: javascript
1919

2020
db.inventory.find( { qty: { $exists: true, $nin: [ 5, 15 ] } } )
21-
21+
2222
This query will select all documents in the ``inventory`` collection
2323
where the ``qty`` field exists *and* its value does not equal either
2424
``5`` nor ``15``.
25-
25+
26+
.. note::
27+
28+
If set to ``false``, the query only returns those documents that
29+
do not contain the field and *not* documents that contain the
30+
field but has the value ``null``.
31+
32+
2633
.. seealso::
27-
28-
:method:`find() <db.collection.find()>`, :operator:`$and`,
29-
:operator:`$nin`, :operator:`$in`.
34+
35+
- :method:`find() <db.collection.find()>`
36+
37+
- :operator:`$nin`
38+
39+
- :operator:`$and`
40+
41+
- :operator:`$in`
42+
43+
- :ref:`faq-developers-query-for-nulls`

0 commit comments

Comments
 (0)