Skip to content

Commit 57187c4

Browse files
committed
DOCS-484 clarify using and the comma when operating on same field
1 parent 1727e25 commit 57187c4

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

source/reference/operator/and.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,21 @@ $and
3939

4040
db.inventory.find( { price: 1.99, qty: { $lt: 20 } , sale: true } )
4141

42-
If, however, a query requires an ``AND`` operation on the same
43-
field, you *must* use the :operator:`$and` operator as in the
44-
following example:
42+
If, however, a query requires an ``AND`` operation on the same field
43+
such as ``{ $price: { $ne: 1.99 } } AND { price: { $exists: true }
44+
}``, then either use the :operator:`$and` operator for the two
45+
separate expressions or combine the operator expressions for the
46+
field ``{ $price: { $ne: 1.99, $exists: true } }``.
4547

48+
Consider the following examples:
49+
4650
.. code-block:: javascript
4751

4852
db.inventory.update( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] }, { $set: { qty: 15 } } )
4953

50-
This :method:`update() <db.collection.update()>` operation will set
54+
db.inventory.update( { price: { $ne: 1.99, $exists: true } } , { $set: { qty: 15 } } )
55+
56+
Both :method:`update() <db.collection.update()>` operations will set
5157
the value of the ``qty`` field in documents where:
5258

5359
- the ``price`` field value does not equal ``1.99`` **and**

source/reference/operator/exists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ $exists
1717

1818
.. code-block:: javascript
1919

20-
db.inventory.find( { $and: [ { qty: { $exists: true } }, { qty: { $nin: [ 5, 15 ] } } ] } )
20+
db.inventory.find( { qty: { $exists: true, $nin: [ 5, 15 ] } } )
2121

2222
This query will select all documents in the ``inventory`` collection
2323
where the ``qty`` field exists *and* its value does not equal either
2424
``5`` nor ``15``.
25-
26-
The above query used the :operator:`$and` operator because the query
27-
performs an ``AND`` operation on the value of the same field and is
28-
not specific to the :operator:`$exists` operator.
2925

3026
.. seealso::
3127

0 commit comments

Comments
 (0)