Skip to content

DOCS-484 #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions source/includes/note-ref-equality.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.. note::

To signify ``equal to`` (``=``), MongoDB defaults to the JSON ``{
key:value }`` structure. For example, the query
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs colon.


.. code-block:: javascript

db.collection.find( { field: value } )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing one spance


selects all the documents where the ``field`` equals ``value``.



37 changes: 27 additions & 10 deletions source/reference/operator/all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,38 @@ $all

.. operator:: $all

The :operator:`$all` operator matches a minimum set of elements that must
be present in a document's ``field``, as in the following example:
*Syntax*: ``{ field: { $all: array }``

:operator:`$all` selects the documents where:
- the ``field`` is *not* an array and its value matches the specified ``array``'s single element **or**
- the ``field`` is an array that contains all the elements in the specified ``array``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix indentation, improve parallelism.


**Example**: Select all documents in ``inventory`` where ``qty``
matches the element of the specified array ``[5]``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs something between the word "array" and [5]...


.. code-block:: javascript

db.inventory.find( { qty: { $all: [ 5 ] } } )

**Example**: Select all documents in ``inventory`` where ``tags``
contains all the elements in the array ``["appliances", "school"]``.

.. code-block:: javascript

db.inventory.find( { tags: { $all: [ "appliances", "school" ] } } )

db.collection.find( { field: { $all: [ 1, 2 , 3 ] } } );
**Example**: Update a single document in ``inventory`` where
``tags`` contain all the elements in the array ``["appliances",
"school"]``.

This returns all documents in ``collection`` where the value of
``field`` is an array that is equivalent to or a superset of ``[
1, 2, 3, ]``. The :operator:`$all` operator will not return any arrays
that are subsets; for example, the above query matches ``{ field: [
1, 2, 3, 4] }`` but not ``{ field: [ 2, 3 ] }``.
.. code-block:: javascript

db.inventory.update( { tags: { $all: [ "appliances", "school" ] } }, { $set: { sale: false } } )

.. note::

In most cases, MongoDB does not treat arrays as sets. This
operator provides a notable exception to this general approach

operator provides a notable exception to this general approach.

See also :method:`find() <db.collection.find()>`, :method:`update()
<db.collection.update()>`, :operator:`$set`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would try to either put this in a .. seealso:: block or make it into a more clear sentence.

43 changes: 36 additions & 7 deletions source/reference/operator/and.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,43 @@ $and

.. versionadded:: 2.0

The :operator:`$and` operator provides a Boolean ``AND`` expression in
queries. Use :operator:`$and` to return the documents that satisfy *all*
included expressions. For example:
*Syntax*: ``{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }``

:operator:`$and` performs a logical ``AND`` operation on the
specified array of ``<expressions>`` and selects the
documents that satisfy *all* the ``<expressions>`` in the array.

:operator:`$and` requires at least two ``<expressions>``. In the above
syntax, ``N`` must be greater than or equal to 2.

**Example**: Select all documents in ``inventory`` where
- ``price`` equals ``1.99`` **and**
- ``qty`` is less than ``20`` **and**
- ``sale`` is ``true``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the indentation of this block doesn't make sense. it can be 3 rather than 6 spaces indented.

where should have a colon after it.


.. code-block:: javascript

db.inventory.find({ $and: [ { price: 1.99 }, { qty: { $lt: 20 } }, { sale: true } ] } )

**Example**: Update a single document in ``inventory`` where
- ``price`` equals ``1.99`` **and**
- ``qty`` is less than ``20``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix indentation as mentioned elsewhere


.. code-block:: javascript

db.inventory.update( { $and: [ { price: 1.99 }, { qty: { $lt: 20 } } ] }, { $set: { qty: 15 } } )

MongoDB queries provide an implicit ``AND`` operation by specifying
a comma-separated list of expressions. The above examples are
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MongoDB provides an implicit AND operation when specifying a comma separated list of fields. Use :operator:$and when you need an AND operation on the value of a single field.

equivalent to:

.. code-block:: javascript

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

db.inventory.update( { price: 1.99, qty: { $lt: 20 } , sale: true }, { $set: { qty: 15 } } )

db.collection.find( { $and [ { key1: value1 }, { key2: value2} ] } );
See also :method:`find() <db.collection.find()>`, :method:`update()
<db.collection.update()>`, :operator:`$set`.

returns all documents in ``collection`` that have *both* a
``key1`` field with ``value1`` *and* a ``key2`` field with
``value2``.

34 changes: 26 additions & 8 deletions source/reference/operator/exists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,37 @@ $exists

.. operator:: $exists

The :operator:`$exists` operator tests documents for the existence
of a field. The :operator:`$exists` operator accepts either true and
false values. For example:
*Syntax*: ``{ field: { $exists: boolean } }``

:operator:`$exists` selects the documents that contain
the field.

MongoDB `$exists` does **not** correspond to SQL operator
``exists``. For SQL ``exists``, refer to the :operator:`$in`.

**Example**: Select all documents in ``inventory`` where ``sale``
exists.

.. code-block:: javascript

db.collection.find( { field: { $exists: true } );
db.inventory.find( { sale: { $exists: true } } )

returns all documents in ``collection`` that have ``field``, while:
**Example**: Select all documents in ``inventory`` where ``qty``
exists *and* is not in the array ``[5, 15]``.

.. code-block:: javascript

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

You **must** use the :operator:`$and` construct rather than the
implicit ``AND`` operation provided by comma-separated list.

**Example**: Update a single document in ``inventory`` where
``sale`` does *not* exist.

.. code-block:: javascript

db.collection.find( { field: { $exists: false } );
db.inventory.update( { sale: { $exists: false } }, { $set: { sale: false } } )

returns all documents in ``collection`` that do *not* have ``field``
specified.
See also :method:`find() <db.collection.find()>`, :method:`update()
<db.collection.update()>`, :operator:`$set`, :operator:`$and`.
27 changes: 17 additions & 10 deletions source/reference/operator/gt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@ $gt

.. operator:: $gt

The :operator:`$gt` comparison operator provides the ability to select
documents where a field is greater than (e.g. ``>``) a value:
*Syntax*: ``{field: {$gt: value} }``

.. code-block:: javascript

db.collection.find( { field: { $gt: value } } );
:operator:`$gt` selects those documents where the ``field`` is greater
than (i.e. ``>``) the specified ``value``.

This query returns all documents in ``collection`` where a value
of ``field`` is greater than the specified ``value``.
**Example**: Select all documents in ``inventory`` where ``qty`` is
greater than ``20``.

.. code-block:: javascript

If ``field`` holds an array, only one value in the array needs to
be greater than the specified ``value`` to produce a successful
match.
db.inventory.find( { qty: { $gt: 20 } } )

**Example**: Update a single document in ``inventory`` where ``qty``
is greater than ``20``.

.. code-block:: javascript

db.inventory.update( { qty: { $gt: 20 } }, { $set: { qty: 0 } } )

See also :method:`find() <db.collection.find()>`, :method:`update()
<db.collection.update()>`, :operator:`$set`.
24 changes: 17 additions & 7 deletions source/reference/operator/gte.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ $gte

.. operator:: $gte

The :operator:`$gte` comparison operator provides the ability to select
documents where a field is greater than or equal to (e.g. ``>=``) a
value:
*Syntax*: ``{field: {$gte: value} }``

.. code-block:: javascript
:operator:`$gte` selects the documents where the ``field`` is greater than or
equal to (i.e. ``>=``) the specified ``value``.

db.collection.find( { field: { $gte: value } } );
**Example**: Select all documents in ``inventory`` where ``qty`` is
greater than or equal to ``20``.

.. code-block:: javascript

This query returns all documents in ``collection`` where the value
of ``field`` greater than or equal to the specified ``value``.
db.inventory.find( { qty: { $gte: 20 } } )

**Example**: Update a single document in ``inventory`` where ``qty``
is greater than or equal to ``20``.

.. code-block:: javascript

db.inventory.update( { qty: { $gte: 20 } }, { $set: { qty: 0 } } )

See also :method:`find() <db.collection.find()>`, :method:`update()
<db.collection.update()>`, :operator:`$set`.
51 changes: 21 additions & 30 deletions source/reference/operator/in.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,34 @@ $in

.. operator:: $in

The :operator:`$in` operator allows you to specify a set of possible
matches for any value. Consider the following form:
*Syntax*: ``{ field: { $in: array } }``

.. code-block:: javascript

db.collection.find( { field: { $in: array } } );

Here, :operator:`$in` returns all documents in ``collection`` where
``field`` has a value included in ``array``. This is analogous to
the ``IN`` modifier in SQL. For example:
:operator:`$in` selects the documents where the ``field`` is in the
specified ``array``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's bad form to have literal phrases (inline) have both technical meaning (as in the <array> value and "array" as in the data structure....


If the ``field`` is an array, then ``{ field: { $in: array } }``
is true *if* at least one element of the ``field`` is found in the ``array``.

**Example**: Select all documents in ``inventory`` where ``qty`` is
in the array ``[5, 15]``.

.. code-block:: javascript

db.collection.find( { age: { $in: [ 1, 2, 3, 5, 7, 11 } } );

returns all documents in ``collection`` with an ``age`` field
that is *one* of the first six prime numbers, including all of the
following documents:
db.inventory.find( { qty: { $in: [ 5, 15 ] } } )

**Example**: Select all documents in ``inventory`` where any element
of the ``tags`` is in ``["appliances", "school"]``.

.. code-block:: javascript

db.inventory.find( { tags: { $in: [ "appliances", "school" ] } } )

{ age: 7 }
{ age: 11 }
{ age: 3 }

When the field that :operator:`$in` inspects (i.e. ``age`` in the
above example) is itself an array, only *one* of the values in the
array must match *one* of the values in the :operator:`$in`
array. Therefore, the following query:

.. code-block:: javascript

db.collection.find( { a: { $in: [1, 2] } } )

will match both of the following documents:
**Example**: Update a single document in ``inventory`` where any
element of the ``tags`` is in ``["appliances", "school"]``.

.. code-block:: javascript

{ a: [ 1, 3, 5, 7, 9 ] }
{ a: [ 0, 2, 4, 6, 8 ] }
db.inventory.update( { tags: { $in: ["appliances", "school"] } }, { $set: { sale:true } } )

See also :method:`find() <db.collection.find()>`, :method:`update()
<db.collection.update()>`, :operator:`$set`.
21 changes: 16 additions & 5 deletions source/reference/operator/lt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@ $lt

.. operator:: $lt

The :operator:`$lt` comparison operator provides the ability to select
documents where a field is less than (e.g. ``<``) a value:
*Syntax*: ``{field: {$lt: value} }``

:operator:`$lt` selects the documents where the ``field`` is less than
(i.e. ``<``) the specified ``value``.

**Example**: Select all documents in ``inventory`` where ``qty``
is less than ``20``.

.. code-block:: javascript

db.collection.find( { field: { $lt: value } } );
db.inventory.find( { qty: { $lt: 20 } } )

This query returns all documents in ``collection`` where a value
of ``field`` is less than the specified ``value``.
**Example**: Update a single document in ``inventory`` where ``qty`` is less than ``20``.

.. code-block:: javascript

db.inventory.update( { qty: { $lt: 20 } }, { $set: { qty: 0 } } )

See also :method:`find() <db.collection.find()>`, :method:`update()
<db.collection.update()>`, :operator:`$set`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revise as above.

22 changes: 16 additions & 6 deletions source/reference/operator/lte.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ $lte

.. operator:: $lte

The :operator:`$lte` comparison operator provides the ability to select
documents where a field is less than or equal to (e.g. ``<=``) a
value:
*Syntax*: ``{ field: { $lte: value} }``

:operator:`$lte` selects the documents where the ``field`` is less than or
equal to (i.e. ``<=``) the specified ``value``.

**Example**: Select all documents in ``inventory`` where ``qty``
is less than or equal to ``20``.

.. code-block:: javascript

db.collection.find( { field: { $lte: value } } );
db.inventory.find( { qty: { $lte: 20 } } )

This query returns all documents in ``collection`` where the value
of ``field`` less than or equal to the specified ``value``.
**Example**: Update a single document in ``inventory`` where ``qty`` is less than or equal to ``20``.

.. code-block:: javascript

db.inventory.update( { qty: { $lte: 20 } }, { $set: { qty: 0 } } )

See also :method:`find() <db.collection.find()>`, :method:`update()
<db.collection.update()>`, :operator:`$set`.
33 changes: 18 additions & 15 deletions source/reference/operator/mod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ $mod

.. operator:: $mod

The :operator:`$mod` operator performs a fast "modulo" query, to
reduce the need for expensive :operator:`$where` operator in some
cases. :operator:`$mod` performs a modulo operation on the value of
a field, and returns all documents that with the specified remainder value. For
example:

*Syntax*: ``{ field: { $mod: [ divisor, remainder ]} }``

:operator:`$mod` selects the documents where the ``field`` value modulo
``divisor`` equals the ``remainder``. In some cases,
:operator:`$mod` can reduce the need for expensive
:operator:`$where` operator in some cases.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to explain the operation a bit more clearly, or introduce it for more clarity.


**Example**: Select all documents in ``inventory`` where the ``qty`` modulo ``4`` equals 3.

.. code-block:: javascript

db.collection.find( { field: { $mod: [ d, m ] } } );

returns all documents in ``collection`` with a remainder of ``m``,
with a divisor of ``d``. This replaces the following
:operator:`$where` operation:


db.inventory.find( { qty: { $mod: [ 4, 0 ] } } )

replaces the more expensive
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this probably needs to fit into the sentence a bit more clearly.


.. code-block:: javascript

db.collection.find( "field % d == m" );

db.inventory.find( { $where: "this.qty % 4 == 3" } )

See also :method:`find() <db.collection.find()>`, :operator:`$where`
Loading