6
6
================================
7
7
Query for Null or Missing Fields
8
8
================================
9
-
9
+
10
10
.. default-domain:: mongodb
11
11
12
+ .. facet::
13
+ :name: programming_language
14
+ :values: shell, csharp, go, java, javascript/typescript, php, python, ruby, scala
15
+
16
+ .. meta::
17
+ :keywords: java sync, java async, reactive streams, motor, code example, node.js, compass
18
+
12
19
.. contents:: On this page
13
20
:local:
14
21
:backlinks: none
@@ -29,7 +36,7 @@ Different query operators in MongoDB treat ``null`` values differently.
29
36
30
37
.. |query_operations| replace:: operations that query for ``null`` values
31
38
32
- .. include:: /includes/driver-examples/driver-example-query-intro.rst
39
+ .. include:: /includes/driver-examples/driver-example-query-intro-no-perl .rst
33
40
34
41
.. tabs-drivers::
35
42
@@ -46,12 +53,6 @@ Different query operators in MongoDB treat ``null`` values differently.
46
53
Use ``None`` with the Motor driver to
47
54
query for ``null`` or missing fields in MongoDB.
48
55
49
- - id: perl
50
- content: |
51
- .. important::
52
- Use ``undef`` with the MongoDB Perl driver to
53
- query for ``null`` or missing fields in MongoDB.
54
-
55
56
- id: ruby
56
57
content: |
57
58
.. important::
@@ -136,12 +137,6 @@ Equality Filter
136
137
contain the ``item`` field whose value is ``null`` *or* that
137
138
do not contain the ``item`` field.
138
139
139
- - id: perl
140
- content: |
141
- The ``{ item => undef }`` query matches documents that either
142
- contain the ``item`` field whose value is ``null`` *or* that
143
- do not contain the ``item`` field.
144
-
145
140
- id: ruby
146
141
content: |
147
142
The ``{ item => nil }`` query matches documents that either
@@ -171,6 +166,95 @@ Equality Filter
171
166
172
167
The query returns both documents in the collection.
173
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
+
174
258
Type Check
175
259
----------
176
260
@@ -241,14 +325,6 @@ Type Check
241
325
:doc:`BSON Type </reference/bson-types>` ``Null``
242
326
(type number ``10``) :
243
327
244
- - id: perl
245
- content: |
246
- The ``{ item => { $type => 10 } }`` query matches *only*
247
- documents that contain the ``item`` field whose value is
248
- ``null``; i.e. the value of the ``item`` field is of
249
- :doc:`BSON Type </reference/bson-types>` ``Null``
250
- (type number ``10``) :
251
-
252
328
- id: ruby
253
329
content: |
254
330
The ``{ item => { $type => 10 } }`` query matches *only*
@@ -336,11 +412,6 @@ field. [#type0]_
336
412
The ``[ item => [ $exists => false ] ]`` query matches documents
337
413
that do not contain the ``item`` field:
338
414
339
- - id: perl
340
- content: |
341
- The ``{ item => { $exists => false } }`` query matches documents
342
- that do not contain the ``item`` field:
343
-
344
415
- id: ruby
345
416
content: |
346
417
The ``{ item => { $exists => false } }`` query matches documents
0 commit comments