Skip to content

Commit 7d7f536

Browse files
Adds how to query for Null or Missing Fields information. (#5821) (#5858)
* new $ne examples; remove perl tabs * another attempt to remove perl * simplify ruby example * review changes * Update source/tutorial/query-for-null-fields.txt --------- Co-authored-by: Jeff Allen <[email protected]>
1 parent 5d4d48c commit 7d7f536

6 files changed

+215
-63
lines changed

source/includes/driver-examples/driver-example-query-38.rst

-9
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,6 @@
7676
:start-after: Start Example 38
7777
:end-before: End Example 38
7878

79-
- id: perl
80-
content: |
81-
82-
.. literalinclude:: /driver-examples/driver-examples.t
83-
:language: perl
84-
:dedent: 4
85-
:start-after: Start Example 38
86-
:end-before: End Example 38
87-
8879
- id: ruby
8980
content: |
9081

source/includes/driver-examples/driver-example-query-39.rst

-9
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,6 @@
7575
:start-after: Start Example 39
7676
:end-before: End Example 39
7777

78-
- id: perl
79-
content: |
80-
81-
.. literalinclude:: /driver-examples/driver-examples.t
82-
:language: perl
83-
:dedent: 4
84-
:start-after: Start Example 39
85-
:end-before: End Example 39
86-
8778
- id: ruby
8879
content: |
8980

source/includes/driver-examples/driver-example-query-40.rst

-9
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,6 @@
7575
:start-after: Start Example 40
7676
:end-before: End Example 40
7777

78-
- id: perl
79-
content: |
80-
81-
.. literalinclude:: /driver-examples/driver-examples.t
82-
:language: perl
83-
:dedent: 4
84-
:start-after: Start Example 40
85-
:end-before: End Example 40
86-
8778
- id: ruby
8879
content: |
8980

source/includes/driver-examples/driver-example-query-41.rst

-9
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,6 @@
7575
:start-after: Start Example 41
7676
:end-before: End Example 41
7777

78-
- id: perl
79-
content: |
80-
81-
.. literalinclude:: /driver-examples/driver-examples.t
82-
:language: perl
83-
:dedent: 4
84-
:start-after: Start Example 41
85-
:end-before: End Example 41
86-
8778
- id: ruby
8879
content: |
8980

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
.. tabs-drivers::
2+
3+
tabs:
4+
- id: shell
5+
content: |
6+
7+
This page provides examples of |query_operations| using the
8+
:method:`db.collection.find()` method in :binary:`mongosh`.
9+
10+
.. include:: /includes/driver-examples/examples-intro.rst
11+
12+
- id: compass
13+
content: |
14+
15+
This page provides examples of |query_operations| using
16+
:ref:`MongoDB Compass <compass-index>`.
17+
18+
.. include:: /includes/driver-examples/examples-intro.rst
19+
20+
- id: python
21+
content: |
22+
23+
This page provides examples of |query_operations| using the
24+
:py:meth:`pymongo.collection.Collection.find` method in the
25+
:api:`PyMongo <pymongo>`
26+
Python driver.
27+
28+
.. include:: /includes/driver-examples/examples-intro.rst
29+
30+
- id: motor
31+
content: |
32+
33+
This page provides examples of |query_operations| using the
34+
:py:meth:`motor.motor_asyncio.AsyncIOMotorCollection.find`
35+
method in the `Motor <https://motor.readthedocs.io/en/stable/>`_
36+
driver.
37+
38+
.. include:: /includes/driver-examples/examples-intro.rst
39+
40+
- id: java-sync
41+
content: |
42+
43+
This page provides examples of |query_operations| using the
44+
com.mongodb.client.MongoCollection.find_ method in the MongoDB
45+
`Java Synchronous Driver`_.
46+
47+
.. tip::
48+
49+
The driver provides com.mongodb.client.model.Filters_
50+
helper methods to facilitate the creation of filter
51+
documents. The examples on this page use these methods to
52+
create the filter documents.
53+
54+
.. include:: /includes/driver-examples/examples-intro.rst
55+
56+
- id: java-async
57+
content: |
58+
59+
This page provides examples of |query_operations| using the
60+
`com.mongodb.reactivestreams.client.MongoCollection.find <http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#find()>`_
61+
method in the MongoDB `Java Reactive Streams Driver <http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/>`_.
62+
63+
.. include:: /includes/driver-examples/examples-intro.rst
64+
65+
- id: nodejs
66+
content: |
67+
68+
This page provides examples of |query_operations| using the
69+
:node-api:`Collection.find() <Collection.html#find>` method in
70+
the :node-docs:`MongoDB Node.js Driver <>`.
71+
72+
.. include:: /includes/driver-examples/examples-intro.rst
73+
74+
- id: php
75+
content: |
76+
77+
This page provides examples of |query_operations| using the
78+
:phpmethod:`MongoDB\\Collection::find() <phpmethod.MongoDB\\Collection::find>`
79+
method in the
80+
`MongoDB PHP Library <https://www.mongodb.com/docs/drivers/php-libraries/>`_.
81+
82+
.. include:: /includes/driver-examples/examples-intro.rst
83+
84+
- id: ruby
85+
content: |
86+
87+
This page provides examples of |query_operations| using the
88+
:ruby-api:`Mongo::Collection#find()<Collection.html#find-instance_method>`
89+
method in the
90+
:ruby:`MongoDB Ruby Driver </>`.
91+
92+
.. include:: /includes/driver-examples/examples-intro.rst
93+
94+
- id: scala
95+
content: |
96+
97+
This page provides examples of |query_operations| using the
98+
:scala-api:`collection.find()<find[C](filter:org.mongodb.scala.bson.conversions.Bson)(implicite:org.mongodb.scala.bson.DefaultHelper.DefaultsTo[C,TResult],implicitct:scala.reflect.ClassTag[C]):org.mongodb.scala.FindObservable[C]>` method
99+
in the
100+
`MongoDB Scala Driver <http://mongodb.github.io/mongo-scala-driver/>`_.
101+
102+
.. include:: /includes/driver-examples/examples-intro.rst
103+
104+
- id: csharp
105+
content: |
106+
107+
This page provides examples of |query_operations| using the
108+
:csharp-api:`MongoCollection.Find() <M_MongoDB_Driver_MongoCollection_1_Find>`
109+
method in the
110+
`MongoDB C# Driver <https://mongodb.github.io/mongo-csharp-driver/>`_.
111+
112+
.. include:: /includes/driver-examples/examples-intro.rst
113+
114+
- id: go
115+
content: |
116+
117+
This page provides examples of |query_operations| using the
118+
:go-api:`Collection.Find <mongo#Collection.Find>`
119+
function in the
120+
`MongoDB Go Driver <https://github.com/mongodb/mongo-go-driver/>`_.
121+
122+
.. include:: /includes/driver-examples/examples-intro.rst
123+

source/tutorial/query-for-null-fields.txt

+92-27
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
================================
77
Query for Null or Missing Fields
88
================================
9-
9+
1010
.. default-domain:: mongodb
1111

1212
.. facet::
1313
: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
1515

1616
.. meta::
1717
: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.
3636

3737
.. |query_operations| replace:: operations that query for ``null`` values
3838

39-
.. include:: /includes/driver-examples/driver-example-query-intro.rst
39+
.. include:: /includes/driver-examples/driver-example-query-intro-no-perl.rst
4040

4141
.. tabs-drivers::
4242

@@ -53,12 +53,6 @@ Different query operators in MongoDB treat ``null`` values differently.
5353
Use ``None`` with the Motor driver to
5454
query for ``null`` or missing fields in MongoDB.
5555

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-
6256
- id: ruby
6357
content: |
6458
.. important::
@@ -143,12 +137,6 @@ Equality Filter
143137
contain the ``item`` field whose value is ``null`` *or* that
144138
do not contain the ``item`` field.
145139

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-
152140
- id: ruby
153141
content: |
154142
The ``{ item => nil }`` query matches documents that either
@@ -178,6 +166,95 @@ Equality Filter
178166

179167
The query returns both documents in the collection.
180168

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+
181258
Type Check
182259
----------
183260

@@ -240,13 +317,6 @@ Type Check
240317
``null``; i.e. the value of the ``item`` field is of
241318
:ref:`BSON Type <bson-types>` ``Null`` (BSON Type 10):
242319

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-
250320
- id: ruby
251321
content: |
252322
The ``{ item => { $type => 10 } }`` query matches *only*
@@ -330,11 +400,6 @@ field. [#type0]_
330400
The ``[ item => [ $exists => false ] ]`` query matches documents
331401
that do not contain the ``item`` field:
332402

333-
- id: perl
334-
content: |
335-
The ``{ item => { $exists => false } }`` query matches documents
336-
that do not contain the ``item`` field:
337-
338403
- id: ruby
339404
content: |
340405
The ``{ item => { $exists => false } }`` query matches documents

0 commit comments

Comments
 (0)