Skip to content

Commit f751c7f

Browse files
Adds how to query for Null or Missing Fields information. (#5821) (#5861)
* Adds how to query for Null or Missing Fields information. (#5821) * 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]> --------- Co-authored-by: Jeff Allen <[email protected]> * remove perl --------- Co-authored-by: Jeff Allen <[email protected]>
1 parent 33d4a9c commit f751c7f

6 files changed

+221
-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

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

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+
1219
.. contents:: On this page
1320
:local:
1421
:backlinks: none
@@ -29,7 +36,7 @@ Different query operators in MongoDB treat ``null`` values differently.
2936

3037
.. |query_operations| replace:: operations that query for ``null`` values
3138

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

3441
.. tabs-drivers::
3542

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

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-
5556
- id: ruby
5657
content: |
5758
.. important::
@@ -136,12 +137,6 @@ Equality Filter
136137
contain the ``item`` field whose value is ``null`` *or* that
137138
do not contain the ``item`` field.
138139

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-
145140
- id: ruby
146141
content: |
147142
The ``{ item => nil }`` query matches documents that either
@@ -171,6 +166,95 @@ Equality Filter
171166

172167
The query returns both documents in the collection.
173168

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+
174258
Type Check
175259
----------
176260

@@ -241,14 +325,6 @@ Type Check
241325
:doc:`BSON Type </reference/bson-types>` ``Null``
242326
(type number ``10``) :
243327

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-
252328
- id: ruby
253329
content: |
254330
The ``{ item => { $type => 10 } }`` query matches *only*
@@ -336,11 +412,6 @@ field. [#type0]_
336412
The ``[ item => [ $exists => false ] ]`` query matches documents
337413
that do not contain the ``item`` field:
338414

339-
- id: perl
340-
content: |
341-
The ``{ item => { $exists => false } }`` query matches documents
342-
that do not contain the ``item`` field:
343-
344415
- id: ruby
345416
content: |
346417
The ``{ item => { $exists => false } }`` query matches documents

0 commit comments

Comments
 (0)