diff --git a/source/includes/note-ref-equality.rst b/source/includes/note-ref-equality.rst new file mode 100644 index 00000000000..3dae2fa131b --- /dev/null +++ b/source/includes/note-ref-equality.rst @@ -0,0 +1,18 @@ +.. note:: + + To express ``equal to`` (e.g. ``=``) in the MongoDB query language, + use JSON ``{ key:value }`` structure. Consider the following + prototype: + + .. code-block:: javascript + + db.collection.find( { field: value } ) + + For example: + + .. code-block:: javascript + + db.collection.find( { a: 42 } ) + + This query selects all the documents the where the ``a`` field + holds a value of ``42``. diff --git a/source/reference/command/compact.txt b/source/reference/command/compact.txt index 6e4b673bf07..1a252bdf71a 100644 --- a/source/reference/command/compact.txt +++ b/source/reference/command/compact.txt @@ -6,52 +6,146 @@ compact .. dbcommand:: compact + .. versionadded:: 2.0 + The :dbcommand:`compact` command rewrites and defragments a single - collection. Additionally, the command forces all indexes on the collection - to be rebuilt. The command has the following syntax: + collection. Additionally, the command drops all indexes at the + beginning of compaction and rebuilds the indexes at the end. + :dbcommand:`compact` is conceptually similar to + :dbcommand:`repairDatabase`, but works on a single collection rather + than an entire database. + + The command has the following syntax: .. code-block:: javascript - { compact: "users" } - - This command compacts the collection named ``users``. Note the - following command behaviors: - - - During a :dbcommand:`compact`, the database blocks all other activity. - - - In a :term:`replica set`, :dbcommand:`compact` will refuse to run on the - primary node unless you also specify ``{ force: true }``. - For example: - - .. code-block:: javascript - - { compact: "collection", force: true } - - - If you have journaling enabled, your data will be safe even - if you kill the operation or restart the server before it has - finished. However, you may have to manually rebuild the indexes. - Without journaling enabled, the :dbcommand:`compact` command is much less safe, - and there are no guarantees made about the safety of your data in the - event of a shutdown or a kill. - - .. warning:: - - Always have an up-to-date backup before performing server - maintenance such as the :dbcommand:`compact` operation. - - - :dbcommand:`compact` requires a small amount of additional disk space while - running but unlike :dbcommand:`repairDatabase` it does *not* free - space equal to the total size of the collection. - - - the :dbcommand:`compact` command blocks until the operation is - complete. - - - :dbcommand:`compact` removes any :term:`padding factor` in the collection, - which may impact performance if documents grow regularly. - - - :dbcommand:`compact` commands do not replicate. They must be run on slaves - and replica set members independently. - - - It is not possible to compact :term:`capped collections ` - because they don't have padding, and documents - cannot grow in these collections. + { compact: } + + You may also specify one of the following options: + + - ``force: true`` + + To run on the primary node in a :term:`replica set`. Otherwise, + the :dbcommand:`compact` command returns an error when invoked on + a :term:`replica set` primary because the command blocks all other + activity. + + .. versionchanged:: 2.2 :dbcommand:`compact` blocks activities only + for its database. + + - ``paddingFactor: `` + + To specify a :term:`padding factor` ranging from 1.0 to 4.0 for + the compacted documents. Default factor is 1.0, specifying no + padding and the maximum padding factor is 4.0. If you do updates + that increase the size of the documents, you will want some + padding, especially if you have several indexes for the collection. + + .. versionadded:: v2.2 + + - ``paddingBytes: `` + + To specify a padding as an absolute number of bytes. Specifying + ``paddingBytes`` can be useful if your documents start small but + then increase in size significantly. For example,if your documents + are initially 40 bytes long and you grow them by 1KB, using + ``paddingBytes: 1024`` might be reasonable since using + ``paddingFactor: 4.0`` would only add 40 * (4.0 - 1) = 120 bytes + of padding. + + .. versionadded:: v2.2 + + It is recommended that you always add at least 100 bytes of padding, + and at least 10% of the doc size. + + .. code-block:: javascript + + db.runCommand ( { compact: 'collection name', paddingBytes: 100, paddingFactor: 1.1 } ) + + .. warning:: + + Always have an up-to-date backup before performing server + maintenance such as the :dbcommand:`compact` operation. + + Note the following command behaviors: + + - :dbcommand:`compact` blocks all other activity (in v2.2, blocks + activities only for its database.) You may view the intermediate + progress either by viewing the the :program:`mongod` log file, or + by running the :method:`db.currentOp()` in another shell instance. + + - :dbcommand:`compact` removes any :term:`padding factor` in the + collection if the command is run without either the + ``paddingFactor`` option or the ``paddingByte`` option. This may + impact performance if the documents grow regularly. However, the + existing paddingFactor statistics is kept for the collection and + will be used for future inserts. + + - :dbcommand:`compact` generally uses less disk space than + :dbcommand:`repairDatabase` and is faster. However,the + :dbcommand:`compact` command is still slow and does block database + activities, so you should run the command during scheduled + maintenance. + + - If you kill the operation by running the :method:`db.killOp(opid) ` or + restart the server before it has finished: + + + If you have journaling enabled, your data will + be safe. However, you may have to manually rebuild the indexes. + + + If you do not have journaling enabled, the :dbcommand:`compact` + command is much less safe, and there are no guarantees made about + the safety of your data in the event of a shutdown or a kill. + + + In either case, much of the existing free space in the + collection may become un-reusable. In this scenario, you should + rerun the compaction to completion to restore the use of this free + space. + + - :dbcommand:`compact` may increase the total size and number of our + data files, expecially when run for the first time. However, this + will not increase the total colletion storage space since storage + size is the amount of data allocated within the database files, + and not the size/number of the files on the file system. + + - :dbcommand:`compact` requires a small amount of additional disk + space while running but unlike :dbcommand:`repairDatabase` it does + *not* free space on the file system. + + - You may also wish to run the :dbcommand:`collstats` command before and + after compaction to see how the storage space changes for the + collection. + + - :dbcommand:`compact` commands do not replicate. When running + compact on a :term:`replica set`: + + + Compact each member separately. + + + Ideally, compaction runs on a secondary. (See option + ``force:true`` above for information regarding compacting the + primary.) + + + If :dbcommand:`compact` runs on a secondary, the secondary will + go into "recovering" state automatically to prevent reads from + being routed to it during compation. Once the compaction is + finished, it will automatically return to secondary state. + + You may refer to the "`partial script for automating step down + and compaction + `_") for an example. + + - :dbcommand:`compact` is a command issued to a :program:`mongod`. + In a sharded environment, run :dbcommand:`compact` on each shard + separately as a maintenance operation. (This will likely change in + the future with other enhancements.) + + - It is not possible to compact :term:`capped collections ` because they don't have padding, and documents cannot + grow in these collections. However, the documents of a + :term:`capped collections ` are not subject o + fragmentation. + + .. seealso:: + + :dbcommand:`repairDatabase` \ No newline at end of file diff --git a/source/reference/operator/all.txt b/source/reference/operator/all.txt index 56770253827..d76b1366a5a 100644 --- a/source/reference/operator/all.txt +++ b/source/reference/operator/all.txt @@ -6,21 +6,54 @@ $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: [ , ... ] }`` + + :operator:`$all` selects the documents where the ``field`` holds an + array and contains all elements (e.g. ````, ````, etc.) + in the array. + + Consider the following example: + + .. code-block:: javascript + + db.inventory.find( { tags: { $all: [ "appliances", "school", "book" ] } } ) + + This query selects all documents in the ``inventory`` collection + where the ``tags`` field contains an array with the elements, + ``appliances``, ``school``, and ``technology``. + Therefore, the above query will match documents in the ``inventory`` + collection that have a ``tags`` field that hold *either* of the + following arrays: + + .. code-block:: javascript + + [ "school", "book", "bag", "headphone", "appliances" ] + [ "appliances", "school", "book" ] + + The :operator:`$all` operator exists to describe and specify arrays + in MongoDB queries. However, you may use the :operator:`$all` + operator to select against a non-array ``field``, as in the + following example: + .. code-block:: javascript + + db.inventory.find( { qty: { $all: [ 50 ] } } ) + + **However**, use the following form to express the same query: - db.collection.find( { field: { $all: [ 1, 2 , 3 ] } } ); + .. code-block:: javascript + + db.inventory.find( { qty: 50 } ) - 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 ] }``. + Both queries will select all documents in the ``inventory`` + collection where the value of the ``qty`` field equals ``50``. .. 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 approach. + + .. seealso:: + :method:`find() `, :method:`update() + `, and :operator:`$set`. diff --git a/source/reference/operator/and.txt b/source/reference/operator/and.txt index a39c02b0817..8a4bc62734b 100644 --- a/source/reference/operator/and.txt +++ b/source/reference/operator/and.txt @@ -8,14 +8,51 @@ $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: [ { }, { } , ... , { } ] }`` + + :operator:`$and` performs a logical ``AND`` operation on an array + of *two or more* expressions (e.g. ````, + ````, etc.) and selects the documents that satisfy + *all* the expressions in the array. + Consider the following example: + .. code-block:: javascript + + db.inventory.find({ $and: [ { price: 1.99 }, { qty: { $lt: 20 } }, { sale: true } ] } ) - db.collection.find( { $and [ { key1: value1 }, { key2: value2} ] } ); + This query will select all documents in the ``inventory`` + collection where: + + - ``price`` field value equals ``1.99`` **and** + - ``qty`` field value is less than ``20`` **and** + - ``sale`` field value is equal to ``true``. - returns all documents in ``collection`` that have *both* a - ``key1`` field with ``value1`` *and* a ``key2`` field with - ``value2``. + MongoDB provides an implicit ``AND`` operation when specifying a + comma separated list of expressions. For example, you may write the + above query as: + + .. code-block:: javascript + + db.inventory.find( { price: 1.99, qty: { $lt: 20 } , sale: true } ) + + If, however, a query requires an ``AND`` operation on the same + field, you *must* use the :operator:`$and` operator as in the + following example: + + .. code-block:: javascript + + db.inventory.update( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] }, { $set: { qty: 15 } } ) + + This :method:`update() ` operation will set + the value of the ``qty`` field in documents where: + + - the ``price`` field value does not equal ``1.99`` **and** + - the ``price`` field exists. + + .. seealso:: + + :method:`find() `, :method:`update() + `, :operator:`$ne`, :operator:`$exists`, + :operator:`$set`. + \ No newline at end of file diff --git a/source/reference/operator/exists.txt b/source/reference/operator/exists.txt index 260c2007994..a0527c88009 100644 --- a/source/reference/operator/exists.txt +++ b/source/reference/operator/exists.txt @@ -6,19 +6,28 @@ $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 } }`` - .. code-block:: javascript - - db.collection.find( { field: { $exists: true } ); + :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` + operator. - returns all documents in ``collection`` that have ``field``, while: + Consider the following example: .. code-block:: javascript - db.collection.find( { field: { $exists: false } ); - - returns all documents in ``collection`` that do *not* have ``field`` - specified. + db.inventory.find( { $and: [ { qty: { $exists: true } }, { qty: { $nin: [ 5, 15 ] } } ] } ) + + This query will select all documents in the ``inventory`` collection + where the ``qty`` field exists *and* its value does not equal either + ``5`` nor ``15``. + + The above query used the :operator:`$and` operator because the query + performs an ``AND`` operation on the value of the same field and is + not specific to the :operator:`$exists` operator. + + .. seealso:: + + :method:`find() `, :operator:`$and`, + :operator:`$nin`, :operator:`$in`. diff --git a/source/reference/operator/gt.txt b/source/reference/operator/gt.txt index 9d9b32c9191..a5da3f704e4 100644 --- a/source/reference/operator/gt.txt +++ b/source/reference/operator/gt.txt @@ -6,17 +6,33 @@ $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 value of 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``. + Consider the following example: + + .. 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 } } ) + This query will select all documents in the ``inventory`` collection + where the ``qty`` field value is greater than ``20``. + + Consider the following example which uses the :operator:`$gt` + operator with a field from an embedded document: + + .. code-block:: javascript + + db.inventory.update( { "carrier.fee": { $gt: 2 } }, { $set: { price: 9.99 } } ) + + This :method:`update() ` operation will set + the value of the ``price`` field in the documents that contain the + embedded document ``carrier`` whose ``fee`` field value is + greater than ``2``. + + .. seealso:: + + :method:`find() `, :method:`update() + `, :operator:`$set`. diff --git a/source/reference/operator/gte.txt b/source/reference/operator/gte.txt index ee189c55df6..4a78d589c66 100644 --- a/source/reference/operator/gte.txt +++ b/source/reference/operator/gte.txt @@ -6,14 +6,34 @@ $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} }`` + :operator:`$gte` selects the documents where the value of the + ``field`` is greater than or equal to (i.e. ``>=``) a specified + value (e.g. ``value``.) + + Consider the following example: + .. code-block:: javascript - db.collection.find( { field: { $gte: value } } ); + db.inventory.find( { qty: { $gte: 20 } } ) - This query returns all documents in ``collection`` where the value - of ``field`` greater than or equal to the specified ``value``. + This query would select all documents in ``inventory`` where + the ``qty`` field value is greater than or equal to ``20``. + + Consider the following example which uses the :operator:`$gte` + operator with a field from an embedded document: + + .. code-block:: javascript + + db.inventory.update( { "carrier.fee": { $gte: 2 } }, { $set: { price: 9.99 } } ) + + This :method:`update() ` operation will set + the value of the ``price`` field that contain the embedded document + ``carrier`` whose``fee`` field value is greater than or equal to + ``2``. + .. seealso:: + + :method:`find() `, :method:`update() + `, :operator:`$set`. diff --git a/source/reference/operator/in.txt b/source/reference/operator/in.txt index 5f3f38af4db..d831a05fcc4 100644 --- a/source/reference/operator/in.txt +++ b/source/reference/operator/in.txt @@ -6,43 +6,42 @@ $in .. operator:: $in - The :operator:`$in` operator allows you to specify a set of possible - matches for any value. Consider the following form: - - .. 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: - + *Syntax*: ``{ field: { $in: [, , ... ] } }`` + + :operator:`$in` selects the documents where the ``field`` value + equals any value in the specified array (e.g. ````, + ````, etc.) + + Consider the following example: + .. 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: - - .. code-block:: javascript - - { 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: - + db.inventory.find( { qty: { $in: [ 5, 15 ] } } ) + + This query will select to select all documents in the ``inventory`` + collection where the ``qty`` field value is either ``5`` or + ``15``. Although you can express this query using the + :operator:`$or` operator, choose the :operator:`$in` operator rather + than the :operator:`$or` operator when performing equality checks on + the same field. + + If the ``field`` holds an array, then the :operator:`$in` operator + selects the documents whose ``field`` holds an array that contains + at least one element that matches a value in the specified array + (e.g. ````, ````, etc.) + + Consider the following example: + .. 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 } } ) + + This :method:`update() ` operation will set + the ``sale`` field value in the ``inventory`` collection where the + ``tags`` field holds an array with at least one element matching an + element in the array ``["appliances", "school"]``. + + .. seealso:: + + method:`find() `, :method:`update() + `, :operator:`$or`, :operator:`$set`. diff --git a/source/reference/operator/lt.txt b/source/reference/operator/lt.txt index f049a64d40a..396a09cc65a 100644 --- a/source/reference/operator/lt.txt +++ b/source/reference/operator/lt.txt @@ -6,12 +6,33 @@ $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 value of the + ``field`` is less than (i.e. ``<``) the specified ``value``. + + Consider the following example: + .. code-block:: javascript - db.collection.find( { field: { $lt: value } } ); + db.inventory.find( { qty: { $lt: 20 } } ) + + This query will select all documents in the ``inventory`` collection + where the ``qty`` field value is less than ``20``. - This query returns all documents in ``collection`` where a value - of ``field`` is less than the specified ``value``. + Consider the following example which uses the :operator:`$lt` + operator with a field from an embedded document: + + .. code-block:: javascript + + db.inventory.update( { "carrier.fee": { $lt: 20 } }, { $set: { price: 9.99 } } ) + + This :method:`update() ` operation will set + the ``price`` field value in the documents that contain the + embedded document ``carrier`` whose ``fee`` field value is less + than ``20``. + + .. seealso:: + + method:`find() `, :method:`update() + `, :operator:`$set`. diff --git a/source/reference/operator/lte.txt b/source/reference/operator/lte.txt index abb20d02b8d..1880d69ec04 100644 --- a/source/reference/operator/lte.txt +++ b/source/reference/operator/lte.txt @@ -6,13 +6,34 @@ $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 value of the + ``field`` is less than or equal to (i.e. ``<=``) the specified + ``value``. + + Consider the following example: + .. code-block:: javascript - db.collection.find( { field: { $lte: value } } ); + db.inventory.find( { qty: { $lte: 20 } } ) + + This query will select all documents in the ``inventory`` collection + where the ``qty`` field value is less than or equal to ``20``. - This query returns all documents in ``collection`` where the value - of ``field`` less than or equal to the specified ``value``. + Consider the following example which uses the :operator:`$lt` + operator with a field from an embedded document: + + .. code-block:: javascript + + db.inventory.update( { "carrier.fee": { $lte: 5 } }, { $set: { price: 9.99 } } ) + + This :method:`update() ` operation will set + the ``price`` field value in the documents that contain the embedded + document ``carrier`` whose ``fee`` field value is less than or equal + to ``5``. + + .. seealso:: + + method:`find() `, :method:`update() + `, :operator:`$set`. diff --git a/source/reference/operator/mod.txt b/source/reference/operator/mod.txt index 47bb492527d..55a51273be1 100644 --- a/source/reference/operator/mod.txt +++ b/source/reference/operator/mod.txt @@ -6,20 +6,37 @@ $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 divided + by the ``divisor`` has the specified ``remainder``. + + Consider the following example: + .. 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 ] } } ) + + This query will select all documents in the ``inventory`` collection + where the ``qty`` field value modulo ``4`` equals ``3``, such as + documents with ``qty`` value equal to ``0`` or ``12``. + + In some cases, you can query using the :operator:`$mod` operator + rather than the more expensive :operator:`$where` operator. Consider + the following example using the :operator:`$mod` operator: + .. code-block:: javascript - - db.collection.find( "field % d == m" ); + + db.inventory.find( { qty: { $mod: [ 4, 3 ] } } ) + + The above query is less expensive than the following query which + uses the :operator:`$where` operator: + + .. code-block:: javascript + + db.inventory.find( { $where: "this.qty % 4 == 3" } ) + + .. seealso:: + + :method:`find() `, :method:`update() + `, :operator:`$set`. diff --git a/source/reference/operator/ne.txt b/source/reference/operator/ne.txt index 242b33bfd0f..dd0c8e98a9d 100644 --- a/source/reference/operator/ne.txt +++ b/source/reference/operator/ne.txt @@ -6,13 +6,36 @@ $ne .. operator:: $ne - The :operator:`$ne` operator returns documents where a field is not - equal to the specified value. The following command: + *Syntax*: ``{field: {$ne: value} }`` + :operator:`$ne` selects the documents where the value of the + ``field`` is not equal (i.e. ``!=``) to the specified ``value``. + This includes documents that do not contain the ``field``. + + Consider the following example: + .. code-block:: javascript - db.collection.find( { field: { $ne: 100 } } ); + db.inventory.find( { qty: { $ne: 20 } } ) - returns all documents in ``collection`` with ``field`` that does not - equal 100. + This query will select all documents in the ``inventory`` collection + where the ``qty`` field value does not equal ``20``, + including those documents that do not contain the ``qty`` field. + + Consider the following example which uses the :operator:`$ne` + operator with a field from an embedded document: + + .. code-block:: javascript + db.inventory.update( { "carrier.state": { $ne: "NY" } }, { $set: { qty: 20 } } ) + + This :method:`update() ` operation will set + the ``qty`` field value in the documents that contains the embedded + document ``carrier`` whose ``state`` field value does not equal "NY", + or where the ``state`` field or the ``carrier`` embedded document + does not exist. + + .. seealso:: + + method:`find() `, :method:`update() + `, :operator:`$set`. diff --git a/source/reference/operator/nin.txt b/source/reference/operator/nin.txt index f1d68a1226a..638dac58916 100644 --- a/source/reference/operator/nin.txt +++ b/source/reference/operator/nin.txt @@ -1,18 +1,47 @@ -=== -$in -=== +==== +$nin +==== .. default-domain:: mongodb .. operator:: $nin - The :operator:`$nin` operator provides a "not in," as the inverse of - :operator:`$in`. For example: + *Syntax*: ``{ field: { $nin: [ , ... ]} }`` + :operator:`$nin` selects the documents where: + + - the ``field`` value is not in the specified ``array`` **or** + - the ``field`` does not exist. + + Consider the following query: + .. code-block:: javascript - db.collection.find( { age: { $nin: [ 3, 5, 7 } } ); + db.inventory.find( { qty: { $nin: [ 5, 15 ] } } ) + + This query will select all documents in the ``inventory`` collection + where the ``qty`` field value does **not** equal ``5`` nor + ``15``. The selected documents will include those documents that do + *not* contain the ``qty`` field. + + If the ``field`` holds an array, then the :operator:`$nin` operator + selects the documents whose ``field`` holds an array with **no** + element equal to a value in the specified array (e.g. ````, + ````, etc.). + + Consider the following query: + + .. code-block:: javascript - returns all documents in ``collection`` where the value of ``age`` - is *not* 3, 5, or 7. + db.inventory.update( { tags: { $nin: [ "appliances", "school" ] } }, { $set: { sale: false } } ) + This :method:`update() ` operation will set + the ``sale`` field value in the ``inventory`` collection where the + ``tags`` field holds an array with **no** elements matching an + element in the array ``["appliances", "school"]`` or where a + document does not contain the ``tags`` field. + + .. seealso:: + + :method:`find() `, :method:`update() + `, :operator:`$set`. \ No newline at end of file diff --git a/source/reference/operator/nor.txt b/source/reference/operator/nor.txt index b8220a3fa90..61e4c2f4265 100644 --- a/source/reference/operator/nor.txt +++ b/source/reference/operator/nor.txt @@ -6,15 +6,64 @@ $nor .. operator:: $nor - The :operator:`$nor` operators provides a Boolean ``NOR`` expression in - queries. :operator:`$nor` is the functional inverse of :operator:`$or`. Use - :operator:`$nor` to exclude documents that have fields with specific - values. For example: + *Syntax*: ``{ $nor: [ { }, { }, ... { } ] }`` + :operator:`$nor` performs a logical ``NOR`` operation on an array of + *two or more* ```` and selects the documents that + **fail** all the ```` in the array. + + Consider the following example: + .. code-block:: javascript - db.collection.find( { $nor [ { key1: value1 }, { key2: value2} ] } ); + db.inventory.find( { $nor: [ { price: 1.99 }, { qty: { $lt: 20 } }, { sale: true } ] } ) - returns all documents in ``collection`` that have *neither* a - ``key1`` field with ``value1`` *nor* a ``key2`` field with - ``value2``. + This query will select all documents in the ``inventory`` collection + where: + + - the ``price`` field value does *not* equal ``1.99`` **and** + - the ``qty`` field value is *not* less than ``20`` **and** + - the ``sale`` field value is *not* equal to ``true`` + + including those documents that do not contain these field(s). + + The exception in returning documents that do not contain the field + in the :operator:`$nor` expression is when the :operator:`$nor` operator is + used with the :operator:`$exists` operator. + + Consider the following query which uses only the :operator:`$nor` operator: + + .. code-block:: javascript + + db.inventory.find( { $nor: [ { price: 1.99 }, { sale: true } ] } ) + + This query will return all documents that: + + - contain the ``price`` field whose value is *not* equal to ``1.99`` + and contain the ``sale`` field whose value *is not* equal to + ``true`` **or** + - contain the ``price`` field whose value is *not* equal to ``1.99`` + *but* do *not* contain the ``sale`` field **or** + - do *not* contain the ``price`` field *but* contain the ``sale`` + field whose value *is not* equal to ``true`` **or** + - do *not* contain the ``price`` field *and* do *not* contain the + ``sale`` field + + Compare that with the following query which uses the + :operator:`$nor` operator with the :operator:`$exists` operator: + + .. code-block:: javascript + + db.inventory.find( { $nor: [ { price: 1.99 }, { price: { $exists: false } }, + { sale: true }, { sale: { $exists: false } } ] } ) + + This query will return all documents that: + + - contain the ``price`` field whose value is *not* equal to ``1.99`` + and contain the ``sale`` field whose value *is not* equal to + ``true`` + + .. seealso:: + + :method:`find() `, :method:`update() + `, :operator:`$set`, :operator:`$exists`. diff --git a/source/reference/operator/not.txt b/source/reference/operator/not.txt index dcc6ddb26f8..280765f38c4 100644 --- a/source/reference/operator/not.txt +++ b/source/reference/operator/not.txt @@ -6,38 +6,68 @@ $not .. operator:: $not - :operator:`$not` is a meta operator used to reverse the operation - of a standard operator. If a document does not match a query statement, - passing that query statement to the :operator:`$not` will return - that document. The operation of :operator:`$not` is consistent with - the behavior of other operators, but may yield unexpected results - with some data types, like arrays. + *Syntax*: ``{ field: { $not: { } } }`` - :operator:`$not` only affects *other operators*, and is unable to - check fields and documents independently. Use :operator:`$ne` to - test the contents of fields directly and :operator:`$nor` for - logical disjunctions. - - Consider the following example of :operator:`$not`: + :operator:`$not` performs a logical ``NOT`` operation on the + specified ```` and selects the documents that + do *not* match the ````. This includes + documents that do not contain the ``field``. + Consider the following query: + .. code-block:: javascript - db.collection.find( { field: { $not: { $type: 2 } } } ); - - This query returns all documents in ``collection`` where ``field`` - is *not* a string, using the :operator:`$type` operator. - - .. note:: - - The :operator:`$not` operator does not support operations with - :operator:`$regex`. - - When using :operator:`$not`, pass all regular expressions using - the native BSON type. For example, consider the following - expression fragment in Python, using the PyMongo driver: - - .. code-block:: python - - { "$not": re.compile("acme.*corp")} - - .. see:: The :operator:`$type` operator, used in the above example. + db.inventory.find( { price: { $not: { $gt: 1.99 } } } ) + + This query will select all documents in the ``inventory`` collection where: + + - the ``price`` field value is less than or equal to ``1.99`` **or** + - the ``price`` field does not exist + + Notice that ``{ $not: { $gt: 1.99 } }`` is different from the + :operator:`$lte` operator. ``{ $lt: 1.99 }`` returns *only* the + documents where ``price`` field exists and its value is less than or + equal to ``1.99``. + + Remember that the :operator:`$not` operator only affects *other + operators* and cannot check fields and documents independently. So, + use the :operator:`$not` operator for logical disjunctions and the + :operator:`$ne` operator to test the contents of fields directly. + + Consider the following behaviors when using the :operator:`$not` + operator: + + - The operation of the :operator:`$not` operator is consistent with + the behavior of other operators but may yield unexpected results + with some data types like arrays. + + - The :operator:`$not` operator does **not** support operations with + the :operator:`$regex` operator. Instead use ``//`` or in your + driver interfaces, use your langauge's regular expression + capability to create regular expression objects. + + Consider the following example whiche uses the pattern match expression ``//``: + + .. code-block:: javascript + + db.inventory.find( { item: { $not: /^p.*/ } } ) + + The query will select all documents in the ``inventory`` + collection where the ``item`` field value does *not* start with + the letter ``p``. + + If using PyMongo's ``re.compile()``, you can write + the above query as: + + .. code-block:: python + + import re + for noMatch in db.inventory.find( { "item": { "$not": re.compile("^p.*") } } ): + print noMatch + + .. seealso:: + + method:`find() `, :method:`update() + `, :operator:`$set`, :operator:`$gt`, + :operator:`$regex`, :api:`PyMongo `, + :term:`driver`. \ No newline at end of file diff --git a/source/reference/operator/or.txt b/source/reference/operator/or.txt index db561884890..e88680f7ebc 100644 --- a/source/reference/operator/or.txt +++ b/source/reference/operator/or.txt @@ -8,29 +8,92 @@ $or .. versionadded:: 1.6 - The :operator:`$or` operator provides a Boolean ``OR`` expression in - queries. Use :operator:`$or` to match documents against two or more - expressions. For example: + .. versionchanged:: 2.0 + You may nest :operator:`$or` operations; however, these + expressions are not as efficiently optimized as top-level. + + *Syntax*: ``{ $or: [ { }, { }, ... , { } ] }`` + + The :operator:`$or` operator performs a logical ``OR`` operation on an array of + *two or more* ```` and selects the documents that satisfy + *at least* one of the ````. + + Consider the following query: + + .. code-block:: javascript + + db.inventory.find( { price:1.99, $or: [ { qty: { $lt: 20 } }, { sale: true } ] } ) + + This query will select all documents in the ``inventory`` collection + where: + + - the ``price`` field value equals ``1.99`` *and* + - either the ``qty`` field value is less than ``20`` **or** the + ``sale`` field value is ``true``. + + Consider the following example which uses the :operator:`$or` operator to select + fields from embedded documents: .. code-block:: javascript + + db.inventory.update( { $or: [ { price:10.99 }, { "carrier.state": "NY"} ] }, { $set: { sale: true } } ) + + This :method:`update() ` operation will set + the value of the ``sale`` field in the documents where + + to update a single document in ``inventory`` where: + + - the ``price`` field value equals ``10.99`` **or** + - the ``carrier`` embedded document contains a field ``state`` whose + value equals ``NY``. + + When using :operator:`$or` with ```` that are equality + checks for the value of the same field, choose the :operator:`$in` + operator over the :operator:`$or` operator. + + Consider the query to select all documents in the ``inventory`` + collection where: + + - either ``price`` field value equals ``1.99`` *or* the ``sale`` + field value equals ``true``, **and** + - either ``qty`` field value equals ``20`` *or* ``qty`` field value + equals ``50``, + + The most effective query would be: + + .. code-block:: javascript + + db.inventory.find ( { $or: [ { price: 1.99 }, { sale: true } ], qty: { $in: [20, 50] } } ) - db.collection.find( { $or [ { key1: value1 }, { key2: value2} ] } ); + Consider the following behaviors when using the :operator:`$or` + operator: + + - When using indexes with :operator:`$or` queries, remember that + each clause of an :operator:`$or` query will execute in parallel. + These clauses can each use their own index. Consider the following + query: - returns all documents in ``collection`` that *either* have a - ``key1`` field with ``value1`` *or* a ``key2`` field with ``value2``. + .. code-block:: javascript - You may specify a field and then use the :operator:`$or` operator to - further narrow results. Consider the following: + db.inventory.find ( { $or: [ { price: 1.99 }, { sale: true } ] } ) - .. code-block:: javascript + For this query, you would create one index on ``price`` ( + ``db.inventory.ensureIndex( { price: 1 } )`` ) and another index + on ``sale`` ( ``db.inventory.ensureIndex( { sale: 1 } )`` ) rather + than a compound index. - db.collection.find( { age: "19", $or [ { key1: value1 }, { key2: value2} ] } ); + - Also, when using the :operator:`$or` operator with the + :method:`sort() ` method in a query, the query will + **not** use the indexes on the :operator:`$or` fields. Consider the following + query which adds a :method:`sort() ` method to the above query: - This query returns all documents in ``collection`` with an ``age`` - field that has the value ``19``, and *either* a ``key1`` field with - ``value1`` *or* a ``key2`` field with ``value2``. + .. code-block:: javascript - .. versionadded:: 2.0 - You may nest :operator:`$or` operations; however, these - expressions are not as efficiently optimized as top-level - :operator:`$or` operations. + db.inventory.find ( { $or: [ { price: 1.99 }, { sale: true } ] } ).sort({item:1}) + + This modified query will not use the index on ``price`` nor the + index on ``sale``. + + .. seealso:: :method:`find() `, + :method:`update() `, :operator:`$set`, + :operator:`$and`, :method:`sort() `. diff --git a/source/reference/operator/type.txt b/source/reference/operator/type.txt index dbfbb254232..3c777ded6db 100644 --- a/source/reference/operator/type.txt +++ b/source/reference/operator/type.txt @@ -6,17 +6,45 @@ $type .. operator:: $type - The :operator:`$type` operator matches field values with a specific data - type. :operator:`$type` operator allows you to narrow results based on any - :term:`BSON` type. For example: + *Syntax*: ``{ field: { $type: } }`` + + :operator:`$type` selects the documents where the *value* of the + ``field`` is the specified :term:`BSON` type. + + Consider the following example: + + .. code-block:: javascript + db.inventory.find( { price: { $type : 1 } } ) + + This query will select all documents in the ``inventory`` collection + where the ``price`` field value is a Double. + + If the ``field`` holds an array, the :operator:`$type` operator + performs the type check against the array elements and **not** the + ``field``. + + Consider the following example where the ``tags`` field holds an array: + .. code-block:: javascript - db.collection.find( { field: { $type: 2 } } ); + db.inventory.find( { tags: { $type : 4 } } ) + + This query will select all documents in the ``inventory`` collection + where the ``tags`` array contains an element that is itself an array. + + If instead you want to determine whether the ``tags`` field is an + array type, use the :operator:`$where` operator: + + .. code-block:: javascript - returns all documents in ``collection`` where the value of - ``field`` is a string. Consider the following chart for the - available types and their corresponding numbers. + db.inventory.find( { $where : "Array.isArray(this.tags)" } ) + + See the :issue:`SERVER-1475` for more information about the + array type. + + Refer to the following table for the available :term:`BSON` types + and their corresponding numbers. ======================= ========== **Type** **Number** @@ -47,25 +75,13 @@ $type .. note:: - Queries for ``MaxKey`` using the :operator:`$type` work as - expected; however, to query for ``MinKey`` you must specify + To query if a field value is a ``MinKey``, you must use the :operator:`$type` with ``-1`` as in the following example: .. code-block:: javascript db.collection.find( { field: { $type: -1 } } ) - Query statements cannot use :operator:`$type` to test arrays - (i.e. ``4``.) Instead, use the :operator:`$where` operator that - resembles the following: - - .. code-block:: javascript - - db.collection.find( { field: { $where : "Array.isArray(this.field)" } } ) - - See the :issue:`SERVER-1475` for more information about the - array type. - .. example:: Consider the following example operation sequence that @@ -99,3 +115,9 @@ $type db.chunks.find( { "min.shardKey": { $type: -1 } } ) .. include:: /includes/warning-mixing-types.rst + + .. seealso:: + + :method:`find() `, :method:`insert() + `, :operator:`$where`, :term:`BSON`, + :term:`shard key`, :term:`shard cluster` . diff --git a/source/reference/operators.txt b/source/reference/operators.txt index 925a6fa00f3..eba6c7e91ac 100644 --- a/source/reference/operators.txt +++ b/source/reference/operators.txt @@ -21,85 +21,58 @@ Query Selectors Comparison ~~~~~~~~~~ -.. include:: operator/lt.txt +.. include:: /includes/note-ref-equality.rst + +.. include:: operator/ne.txt :start-after: mongodb -.. include:: operator/gt.txt +.. include:: operator/lt.txt :start-after: mongodb .. include:: operator/lte.txt :start-after: mongodb + +.. include:: operator/gt.txt + :start-after: mongodb .. include:: operator/gte.txt :start-after: mongodb +.. include:: operator/in.txt + :start-after: mongodb + +.. include:: operator/nin.txt + :start-after: mongodb + +.. include:: operator/all.txt + :start-after: mongodb + .. TODO move the following note into a querying page, and link in every above included query operator page. -You may combine comparison operators to specify ranges: + You may combine comparison operators to specify ranges: -.. code-block:: javascript + .. code-block:: javascript - db.collection.find( { field: { $gt: value1, $lt: value2 } } ); + db.collection.find( { field: { $gt: value1, $lt: value2 } } ); -This statement returns all documents with ``field`` between -``value1`` and ``value2``. + This statement returns all documents with ``field`` between + ``value1`` and ``value2``. -.. note:: + .. note:: Fields containing arrays match conditional operators, if only one item matches. Therefore, the following query: - .. code-block:: javascript - - db.collection.find( { field: { $gt:0, $lt:2 } } ); - - Will match a document that contains the following field: - - .. code-block:: javascript - - { field: [-1,3] } - -.. index:: query selectors; document -.. _query-selectors-document: - -Document -~~~~~~~~ - -.. include:: operator/all.txt - :start-after: mongodb - -.. include:: operator/exists.txt - :start-after: mongodb - -.. include:: operator/ne.txt - :start-after: mongodb - -.. include:: operator/in.txt - :start-after: mongodb - -.. include:: operator/nin.txt - :start-after: mongodb - -.. index:: query selectors; geospatial -.. _query-selectors-geospatial: -.. _geospatial-query-operators: -.. _geolocation-operators: - -Geospatial -~~~~~~~~~~ + .. code-block:: javascript -.. include:: operator/near.txt - :start-after: mongodb + db.collection.find( { field: { $gt:0, $lt:2 } } ); -.. include:: operator/maxDistance.txt - :start-after: mongodb + Will match a document that contains the following field: -.. include:: operator/within.txt - :start-after: mongodb + .. code-block:: javascript -.. include:: operator/uniqueDocs.txt - :start-after: mongodb + { field: [-1,3] } .. index:: query selectors; logical .. _query-selectors-logical: @@ -107,13 +80,13 @@ Geospatial Logical ~~~~~~~ -.. include:: operator/or.txt +.. include:: operator/and.txt :start-after: mongodb -.. include:: operator/nor.txt +.. include:: operator/or.txt :start-after: mongodb -.. include:: operator/and.txt +.. include:: operator/nor.txt :start-after: mongodb .. include:: operator/not.txt @@ -125,10 +98,10 @@ Logical Element ~~~~~~~ -.. include:: operator/type.txt +.. include:: operator/exists.txt :start-after: mongodb -.. include:: operator/regex.txt +.. include:: operator/type.txt :start-after: mongodb .. include:: operator/mod.txt @@ -143,6 +116,29 @@ JavaScript .. include:: operator/where.txt :start-after: mongodb +.. include:: operator/regex.txt + :start-after: mongodb + +.. index:: query selectors; geospatial +.. _query-selectors-geospatial: +.. _geospatial-query-operators: +.. _geolocation-operators: + +Geospatial +~~~~~~~~~~ + +.. include:: operator/near.txt + :start-after: mongodb + +.. include:: operator/maxDistance.txt + :start-after: mongodb + +.. include:: operator/within.txt + :start-after: mongodb + +.. include:: operator/uniqueDocs.txt + :start-after: mongodb + .. index:: query selectors; array .. _query-selectors-array: