/
query-for-null-fields.txt
273 lines (215 loc) · 8.89 KB
/
query-for-null-fields.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
.. include:: /includes/java-sync-links.rst
.. include:: /includes/java-async-links.rst
.. _faq-developers-query-for-nulls:
================================
Query for Null or Missing Fields
================================
.. default-domain:: mongodb
Different query operators in MongoDB treat ``null`` values differently.
.. |query_operations| replace:: operations that query for ``null`` values
.. tabs-top::
.. include:: /includes/driver-example-query-intro.rst
.. tabs-drivers::
tabs:
- id: python
content: |
.. important::
You must use ``None`` with the PyMongo Python driver to
query for ``null`` or missing fields in MongoDB.
- id: perl
content: |
.. important::
You must use ``undef`` with the MongoDB Perl driver to
query for ``null`` or missing fields in MongoDB.
- id: ruby
content: |
.. important::
You must use ``nil`` with the MongoDB Ruby driver to
query for ``null`` or missing fields in MongoDB.
- id: scala
content: |
.. important::
You must use ``BsonNull()`` with the MongoDB Scala driver to query
for ``null`` or missing fields in MongoDB.
- id: csharp
content: |
.. important::
You must use ``BsonNull.Value`` with the MongoDB C# driver to
query for ``null`` or missing fields in MongoDB.
.. include:: /includes/driver-example-query-38.rst
.. _faq-comparison-with-null:
Equality Filter
---------------
.. tabs-drivers::
tabs:
- id: shell
content: |
The ``{ item : null }`` query matches documents that either
contain the ``item`` field whose value is ``null`` *or* that
do not contain the ``item`` field.
- id: python
content: |
The ``{ item : None }`` query matches documents that either
contain the ``item`` field whose value is ``null`` *or* that
do not contain the ``item`` field.
- id: java-sync
content: |
The ``eq("item", null)`` query matches documents that either
contain the ``item`` field whose value is ``null`` *or* that
do not contain the ``item`` field.
- id: java-async
content: |
The ``eq("item", null)`` query matches documents that either
contain the ``item`` field whose value is ``null`` *or* that
do not contain the ``item`` field.
- id: nodejs
content: |
The ``{ item : null }`` query matches documents that either
contain the ``item`` field whose value is ``null`` *or* that
do not contain the ``item`` field.
- id: php
content: |
The ``[ item => undef ]`` query matches documents that either
contain the ``item`` field whose value is ``null`` *or* that
do not contain the ``item`` field.
- id: perl
content: |
The ``{ item => undef }`` query matches documents that either
contain the ``item`` field whose value is ``null`` *or* that
do not contain the ``item`` field.
- id: ruby
content: |
The ``{ item => nil }`` query matches documents that either
contain the ``item`` field whose value is ``nil`` *or* that
do not contain the ``item`` field.
- id: scala
content: |
The ``equal("item", BsonNull)`` query matches documents that
either contain the ``item`` field whose value is ``null`` *or*
that do not contain the ``item`` field.
- id: csharp
content: |
The ``Eq("item", BsonNull.Value)`` query using the :csharp-api:`FilterDefinitionBuilder.Eq()
<Overload_MongoDB_Driver_FilterDefinitionBuilder_1_Eq>` method
matches documents that either contain the ``item`` field whose
value is ``null`` *or* that do not contain the ``item`` field.
For example, the following query returns both documents:
.. include:: /includes/driver-example-query-39.rst
Type Check
----------
.. tabs-drivers::
tabs:
- id: shell
content: |
The ``{ item : { $type: 10 } }`` query matches documents that
contains the ``item`` field whose value is ``null`` *only*;
i.e. the value of the ``item`` field is of BSON Type ``Null``
(i.e. ``10``) :
- id: python
content: |
The ``{ item : { $type: 10 } }`` query matches documents that
contains the ``item`` field whose value is ``null`` *only*;
i.e. the value of the ``item`` field is of BSON Type ``Null``
(i.e. ``10``) :
- id: java-sync
content: |
The ``type("item", BsonType.NULL)`` query matches documents
that contains the ``item`` field whose value is ``null``
*only*; i.e. the value of the ``item`` field is of BSON Type
``Null`` (i.e. ``10``) :
- id: java-async
content: |
The ``type("item", BsonType.NULL)`` query matches documents
that contains the ``item`` field whose value is ``null``
*only*; i.e. the value of the ``item`` field is of BSON Type
``Null`` (i.e. ``10``) :
- id: nodejs
content: |
The ``{ item : { $type: 10 } }`` query matches documents that
contains the ``item`` field whose value is ``null`` *only*;
i.e. the value of the ``item`` field is of BSON Type ``Null``
(i.e. ``10``) :
- id: php
content: |
The ``[ item => [ $type => 10 ] ]`` query matches documents that
contains the ``item`` field whose value is ``null`` *only*;
i.e. the value of the ``item`` field is of BSON Type ``Null``
(i.e. ``10``) :
- id: perl
content: |
The ``{ item => { $type => 10 } }`` query matches documents that
contains the ``item`` field whose value is ``null`` *only*;
i.e. the value of the ``item`` field is of BSON Type ``Null``
(i.e. ``10``) :
- id: ruby
content: |
The ``{ item => { $type => 10 } }`` query matches documents that
contains the ``item`` field whose value is ``nil`` *only*;
i.e. the value of the ``item`` field is of BSON Type ``Null``
(i.e. ``10``) :
- id: java-sync
content: |
The ``type("item", BsonType.NULL)`` query matches documents
that contains the ``item`` field whose value is ``null``
*only*; i.e. the value of the ``item`` field is of BSON Type
``Null`` (i.e. ``10``) :
- id: csharp
content: |
The ``Type("item", BsonType.Null)`` query using the :csharp-api:`FilterDefinitionBuilder.Type()
<Overload_MongoDB_Driver_FilterDefinitionBuilder_1_Type>` method
matches documents that contain the ``item`` field whose value is
``null`` *only*; i.e. the value of the ``item`` field is of BSON
Type ``Null`` (i.e. ``10``) :
.. include:: /includes/driver-example-query-40.rst
The query returns only the document where the ``item`` field has a
``null`` value.
Existence Check
---------------
.. tabs-drivers::
tabs:
- id: shell
content: |
The ``{ item : { $exists: false } }`` query matches documents
that do not contain the ``item`` field:
- id: python
content: |
The ``{ item : { $exists: False } }`` query matches documents
that do not contain the ``item`` field:
- id: java-sync
content: |
The ``exists("item", false)`` query matches documents that
do not contain the ``item`` field:
- id: java-async
content: |
The ``exists("item", false)`` query matches documents that do
not contain the ``item`` field:
- id: nodejs
content: |
The ``{ item : { $exists: false } }`` query matches documents
that do not contain the ``item`` field:
- id: php
content: |
The ``[ item => [ $exists => false ] ]`` query matches documents
that do not contain the ``item`` field:
- id: perl
content: |
The ``{ item => { $exists => false } }`` query matches documents
that do not contain the ``item`` field:
- id: ruby
content: |
The ``{ item => { $exists => false } }`` query matches documents
that do not contain the ``item`` field:
- id: scala
content: |
The ``exists("item", exists = false)`` query matches documents
that do not contain the ``item`` field:
- id: csharp
content: |
The ``Exists("item", false)`` query using the :csharp-api:`FilterDefinitionBuilder.Exists()
<Overload_MongoDB_Driver_FilterDefinitionBuilder_1_Exists>`
method matches documents that do not contain the ``item`` field:
.. include:: /includes/driver-example-query-41.rst
The query returns only the document that does *not* contain the
``item`` field:
.. seealso:: The reference documentation for the :query:`$type` and
:query:`$exists` operators.