Skip to content

DOCS-1053 array and compound conditional operators #904

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 1 commit into from
Closed
Changes from all commits
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
38 changes: 32 additions & 6 deletions source/reference/operators.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,44 @@ This statement returns all documents with ``field`` between

.. note::

Fields containing arrays match conditional operators, if only one
item matches. Therefore, the following query:
If the field contains an array and the query has multiple
conditional operators, the field as a whole will match if either a
single array element meets the conditions or a combination of array
elements meet the conditions.

.. example:: Query a field that contains an array.

A collection ``students`` contains the following documents where the
``score`` field contains an array of values:

.. code-block:: javascript

db.collection.find( { field: { $gt:0, $lt:2 } } );
{ "_id" : 1, "score" : [ -1, 3 ] }
{ "_id" : 2, "score" : [ 1, 5 ] }
{ "_id" : 3, "score" : [ 5, 5 ] }

Will match a document that contains the following field:
Then, the following query:

.. code-block:: javascript
.. code-block:: javascript

db.students.find( { score: { $gt: 0, $lt: 2 } } )

Will match the following documents:

.. code-block:: javascript

{ "_id" : 1, "score" : [ -1, 3 ] }
{ "_id" : 2, "score" : [ 1, 5 ] }

- In the document with ``_id`` equal to ``1``, the ``score: [ -1, 3
]`` as a whole meets the specified conditions since the element
``-1`` meets the ``$lt: 2`` condition and the element ``3`` meets
the ``$gt: 0`` condition.

{ field: [-1,3] }
- In the document with ``_id`` equal to ``2``, the ``score: [ 1, 5
]`` as a whole meets the specified conditions since the element
``1`` meets both the ``$lt: 2`` condition and the ``$gt: 0``
condition.

.. index:: query selectors; logical
.. _query-selectors-logical:
Expand Down