6
6
================================
7
7
Query for Null or Missing Fields
8
8
================================
9
-
9
+
10
10
.. default-domain:: mongodb
11
11
12
12
.. facet::
13
13
:name: programming_language
14
- :values: shell, csharp, go, java, javascript/typescript, perl, php, python, ruby, scala
14
+ :values: shell, csharp, go, java, javascript/typescript, php, python, ruby, scala
15
15
16
16
.. meta::
17
17
:keywords: java sync, java async, reactive streams, motor, code example, node.js, compass
@@ -36,7 +36,7 @@ Different query operators in MongoDB treat ``null`` values differently.
36
36
37
37
.. |query_operations| replace:: operations that query for ``null`` values
38
38
39
- .. include:: /includes/driver-examples/driver-example-query-intro.rst
39
+ .. include:: /includes/driver-examples/driver-example-query-intro-no-perl .rst
40
40
41
41
.. tabs-drivers::
42
42
@@ -53,12 +53,6 @@ Different query operators in MongoDB treat ``null`` values differently.
53
53
Use ``None`` with the Motor driver to
54
54
query for ``null`` or missing fields in MongoDB.
55
55
56
- - id: perl
57
- content: |
58
- .. important::
59
- Use ``undef`` with the MongoDB Perl driver to
60
- query for ``null`` or missing fields in MongoDB.
61
-
62
56
- id: ruby
63
57
content: |
64
58
.. important::
@@ -143,12 +137,6 @@ Equality Filter
143
137
contain the ``item`` field whose value is ``null`` *or* that
144
138
do not contain the ``item`` field.
145
139
146
- - id: perl
147
- content: |
148
- The ``{ item => undef }`` query matches documents that either
149
- contain the ``item`` field whose value is ``null`` *or* that
150
- do not contain the ``item`` field.
151
-
152
140
- id: ruby
153
141
content: |
154
142
The ``{ item => nil }`` query matches documents that either
@@ -178,6 +166,95 @@ Equality Filter
178
166
179
167
The query returns both documents in the collection.
180
168
169
+ Non-Equality Filter
170
+ -------------------
171
+
172
+ To query for fields that **exist** and are **not null**, use the :query:`$ne`
173
+ operator. The ``{ item : { $ne : null } }`` query matches documents where the
174
+ ``item`` field exists *and* has a non-null value.
175
+
176
+ .. tabs-drivers::
177
+
178
+ tabs:
179
+ - id: shell
180
+ content: |
181
+ .. code-block:: sh
182
+
183
+ db.inventory.find( { item: { $ne : null } } )
184
+
185
+ - id: compass
186
+ content: |
187
+ .. code-block:: javascript
188
+
189
+ { item: { $ne : null } }
190
+
191
+ - id: python
192
+ content: |
193
+ .. code-block:: python
194
+
195
+ cursor = db.inventory.find( { "item": { "$ne": None } } )
196
+
197
+ - id: motor
198
+ content: |
199
+ .. code-block:: python
200
+
201
+ cursor = db.inventory.find( { "item": { "$ne": None } } )
202
+
203
+ - id: java-sync
204
+ content: |
205
+ .. code-block:: java
206
+
207
+ collection.find($ne("item", null));
208
+
209
+ - id: java-async
210
+ content: |
211
+ .. code-block:: java
212
+
213
+ db.inventory.find( { item: { $ne : nul l} } )
214
+
215
+ - id: nodejs
216
+ content: |
217
+ .. code-block:: javascript
218
+
219
+ const cursor = db.collection('inventory')
220
+ .find({ item: { $ne : null }
221
+ });
222
+
223
+ - id: php
224
+ content: |
225
+ .. code-block:: php
226
+
227
+ $cursor = $db->inventory->find(['item' => ['$ne' => null ]]);
228
+
229
+ - id: ruby
230
+ content: |
231
+ .. code-block:: ruby
232
+
233
+ client[:inventory].find(item: { '$ne' => nil })
234
+
235
+ - id: scala
236
+ content: |
237
+ .. code-block:: scala
238
+
239
+ collection.find($ne("item", null));
240
+
241
+ - id: csharp
242
+ content: |
243
+ .. code-block:: csharp
244
+
245
+ var filter = Builders<BsonDocument>.Filter.Ne("item", BsonNull.Value);
246
+ var result = collection.Find(filter).ToList();
247
+
248
+ - id: go
249
+ content: |
250
+ .. code-block:: go
251
+
252
+ cursor, err := coll.Find(
253
+ context.TODO(),
254
+ bson.D{
255
+ {"item", bson.D{"$ne": nil}},
256
+ })
257
+
181
258
Type Check
182
259
----------
183
260
@@ -240,13 +317,6 @@ Type Check
240
317
``null``; i.e. the value of the ``item`` field is of
241
318
:ref:`BSON Type <bson-types>` ``Null`` (BSON Type 10):
242
319
243
- - id: perl
244
- content: |
245
- The ``{ item => { $type => 10 } }`` query matches *only*
246
- documents that contain the ``item`` field whose value is
247
- ``null``; i.e. the value of the ``item`` field is of
248
- :ref:`BSON Type <bson-types>` ``Null`` (BSON Type 10):
249
-
250
320
- id: ruby
251
321
content: |
252
322
The ``{ item => { $type => 10 } }`` query matches *only*
@@ -330,11 +400,6 @@ field. [#type0]_
330
400
The ``[ item => [ $exists => false ] ]`` query matches documents
331
401
that do not contain the ``item`` field:
332
402
333
- - id: perl
334
- content: |
335
- The ``{ item => { $exists => false } }`` query matches documents
336
- that do not contain the ``item`` field:
337
-
338
403
- id: ruby
339
404
content: |
340
405
The ``{ item => { $exists => false } }`` query matches documents
0 commit comments