@@ -427,6 +427,68 @@ for additional information.
427
427
- The :source:`jsobj.h <src/mongo/db/jsobj.h>` source file for the
428
428
definition of ``MinKey`` and ``MaxKey``.
429
429
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
+
430
492
.. _faq-developers:
431
493
432
494
.. _faq-restrictions-on-collection-names:
0 commit comments