From 9faed8aacf68c4bf0e5e5b0062ec840aeef65def Mon Sep 17 00:00:00 2001 From: kay Date: Mon, 22 Apr 2013 17:38:48 -0400 Subject: [PATCH] DOCS-1053 array and compound conditional operators --- source/reference/operators.txt | 38 ++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/source/reference/operators.txt b/source/reference/operators.txt index 385f7a05c36..ea4b8daa3bf 100644 --- a/source/reference/operators.txt +++ b/source/reference/operators.txt @@ -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: