@@ -10,7 +10,7 @@ modifying data are read operations. In MongoDB, all read operations or
1010:term:`documents` that match the query criteria in a collection.
1111
1212This document describes the syntax and structure of queries, which
13- describe how applications request how MongoDB performs read
13+ determines how applications make requests to MongoDB to perform read
1414operations and how different factors affect the efficiency of reads.
1515
1616.. note::
@@ -28,39 +28,38 @@ operations and how different factors affect the efficiency of reads.
2828Queries in MongoDB
2929------------------
3030
31- In the :program:`mongo` shell the :method:`find()
31+ In the :program:`mongo` shell, the :method:`find()
3232<db.collection.find()>` and :method:`findOne()
3333<db.collection.findOne()>` methods perform read operations. The
34- :method:`find() <db.collection.find()>` method is has the following
34+ :method:`find() <db.collection.find()>` method has the following
3535syntax:
3636
3737.. code-block:: javascript
3838
3939 db.collection.find( <query>, <projection> )
4040
41- - The ``db.collection`` object, describes the database and collection
41+ - The ``db.collection`` object specifies the database and collection
4242 to query. All queries in MongoDB address a *single* collection.
4343
4444 You can enter ``db`` in the :program:`mongo` shell to return the
4545 name of the current database. Use the ``show collections`` operation
4646 in the :program:`mongo` shell to list the current collections in the
4747 database.
4848
49- - Queries in MongoDB have a JSON-like syntax, and take the form of a
50- :term:`document` using a collection of :doc:`/reference/ operators`
51- query operators to describe query parameters.
49+ - Queries in MongoDB have a JSON-like syntax and take the form of a
50+ :term:`document` using a set of :doc:`query operators
51+ </reference/ operators>` to describe query parameters.
5252
5353 The ``<query>`` argument of the :method:`find()
54- <db.collection.find()>` method holds this query document. A query,
55- without a query document will return all documents in the
54+ <db.collection.find()>` method holds this query document. A read
55+ operation without a query document will return all documents in the
5656 collection.
5757
58- - The ``<projection>`` argument describes describes the result or
59- return set in the form of a document. Projections specify or limit
60- the fields to return.
58+ - The ``<projection>`` argument describes the result set in the form of
59+ a document. Projections specify or limit the fields to return.
6160
62- Without a projection the operation will return all
63- fields of all documents, specify a projection if your documents are
61+ Without a projection, the operation will return all
62+ fields of the documents. Specify a projection if your documents are
6463 larger, or when your application only needs a subset of available
6564 fields.
6665
@@ -94,7 +93,7 @@ operators, refer to the :doc:`/applications/read` page of the
9493Query Document
9594~~~~~~~~~~~~~~
9695
97- This section provides an overview of query document for MongoDB
96+ This section provides an overview of the query document for MongoDB
9897queries. See the preceding section for more information on
9998:ref:`queries in MongoDB <read-operations-query-operations>`.
10099
@@ -112,7 +111,7 @@ collection of documents named ``inventory``:
112111
113112 Not specifying a query document to the :method:`find()
114113 <db.collection.find()>` is equivalent to specifying an empty query
115- document. Therefore the following operation is equivelent to the
114+ document. Therefore the following operation is equivalent to the
116115 previous operation:
117116
118117 .. code-block:: javascript
@@ -123,26 +122,25 @@ collection of documents named ``inventory``:
123122 field has a certain value. These are simple "equality" queries.
124123
125124 In the following example, the query selects all documents in the
126- collection where the ``type`` field has the value ``snacks``:
125+ collection where the ``type`` field has the value ``' snacks' ``:
127126
128127 .. code-block:: javascript
129128
130129 db.inventory.find( { type: "snacks" } )
131130
132- When the field holds an array, these equality matches select
133- documents if only *one* of the elements in the array match the
134- specified value. In the following example, the query will match all
135- documents where the array in the ``tags`` field contains the element
136- ``fruit``:
131+ When the field holds an array, these equality matches documents if at
132+ least *one* element in the array match the specified value. In the
133+ following example, the query will match all documents where the array
134+ in the ``tags`` field contains the element ``'fruit'``:
137135
138136 .. code-block:: javascript
139137
140138 db.inventory.find( { tags: "fruit" } )
141139
142140- A single-clause query document can also select all documents in a
143- collection given a condition or set of conditions for one field
144- in the collection's documents. Specify conditions in a MongoDB
145- query, using the :ref:`query operators < query-operators>` .
141+ collection given a condition or set of conditions for one field in
142+ the collection's documents. Use the :ref:`query operators
143+ < query-operators>` to specify conditions in a MongoDB query.
146144
147145 In the following example, the query selects all documents in the
148146 collection where the value of the ``type`` field is either
@@ -160,10 +158,9 @@ collection of documents named ``inventory``:
160158 same field.
161159
162160- A compound query can specify conditions for more than one field in
163- the collection's documents. Implicitly, a logical ``AND``
164- conjunction connects the clauses of a compound query so that all
165- specified conditions for a query must be true for the query to
166- select a document.
161+ the collection's documents. Implicitly, a logical ``AND`` conjunction
162+ connects the clauses of a compound query so that the query selects
163+ the documents in the collection that match all the conditions.
167164
168165 In the following example, the query document specifies an equality
169166 match on a single field, followed by a range of values for a second
@@ -174,13 +171,13 @@ collection of documents named ``inventory``:
174171 db.inventory.find( { type: 'food', price: { $lt: 9.95 } } )
175172
176173 This query selects all documents where the ``type`` field has the
177- value ``food`` **and** the value of the ``price`` field is less than
174+ value ``' food' `` **and** the value of the ``price`` field is less than
178175 (i.e. :operator:`$lt`) ``9.95``.
179176
180- - Using the :operator:`$or` you can specify a compound query that
181- joins each clause with a logical ``OR`` conjunction so that the
182- query will select documents that match only one of the query's
183- clauses .
177+ - Using the :operator:`$or` operator, you can specify a compound query
178+ that joins each clause with a logical ``OR`` conjunction so that the
179+ query selects the documents in the collection that match at least one
180+ condition .
184181
185182 In the following example, the query document selects all documents
186183 in the collection where the field ``qty`` has a value greater than
@@ -216,9 +213,9 @@ documentation of all query operators.
216213Result Projections
217214~~~~~~~~~~~~~~~~~~
218215
219- The :term:`projection` specification, limits the fields to return for
216+ The :term:`projection` specification limits the fields to return for
220217all matching documents. Constraining the result set by restricting the
221- fields to return, can minimize network transit costs and the costs of
218+ fields to return can minimize network transit costs and the costs of
222219deserializing documents in the application layer.
223220
224221The second argument to the :method:`find() <db.collection.find()>`
@@ -233,7 +230,7 @@ included, unless explicitly excluded.
233230 You cannot combine inclusion and exclusion semantics in a single
234231 projection with the *exception* of the ``_id`` field.
235232
236- Consider the following projection specifications, in :method:`find()
233+ Consider the following projection specifications in :method:`find()
237234<db.collection.find()>` operations:
238235
239236- If you specify no projection, the :method:`find()
@@ -245,12 +242,12 @@ Consider the following projection specifications, in :method:`find()
245242 db.inventory.find( { type: 'food' } )
246243
247244 This operation will return all documents in the ``inventory``
248- collection where the value of the ``type`` field is ``food``.
245+ collection where the value of the ``type`` field is ``' food' ``.
249246
250247- A project can explicitly include several fields. In the following
251248 operation, :method:`find() <db.collection.find()>` method returns
252249 all documents that match the query as well as ``item`` and ``qty``
253- fields. The results also include the ``_id``, implicitly :
250+ fields. The results also include the ``_id`` field :
254251
255252 .. code-block:: javascript
256253
@@ -274,7 +271,7 @@ Consider the following projection specifications, in :method:`find()
274271 db.inventory.find( { type: 'food' }, { type:0 } )
275272
276273 This operation returns all documents where the value of the ``type``
277- field is ``food``, but does not include the ``type`` field in the
274+ field is ``' food' ``, but does not include the ``type`` field in the
278275 output.
279276
280277 With the exception of the ``_id`` field you cannot combine inclusion
@@ -324,7 +321,7 @@ Measuring Index Use
324321~~~~~~~~~~~~~~~~~~~
325322
326323The :method:`explain() <cursor.explain()>` cursor method allows you to
327- introspect the operation of the query system, and is useful for
324+ inspect the operation of the query system, and is useful for
328325analyzing the efficiency of queries, and for determining how the query
329326uses the index. Call the :method:`explain() <cursor.explain()>` method
330327on another method that returns a cursor, as in the following example:
@@ -341,7 +338,7 @@ on another method that returns a cursor, as in the following example:
341338 plans, it does not reflect accurate query performance.
342339
343340If the above operation could not use an index, the output of
344- :method:`explain() <cursor.explain()>`, would resemble the following:
341+ :method:`explain() <cursor.explain()>` would resemble the following:
345342
346343.. code-block:: javascript
347344
@@ -414,7 +411,7 @@ the query used an index. This query:
414411 the :data:`nscannedObjects` field.
415412
416413 This indicates that the query was not "covered," or able to complete
417- only using the index, as reflected in the :data:`indexOnly` See
414+ only using the index, as reflected in the :data:`indexOnly`. See
418415 :ref:`indexes-covered-queries` for more information.
419416
420417.. index:: query optimizer
@@ -439,7 +436,7 @@ To create a new query plan, the query optimizer:
439436 If an index returns a result already returned by another index, the
440437 optimizer skips the duplicate match.
441438
442- #. determines a "winning" index when either of the following occur:
439+ #. selects an index when either of the following occur:
443440
444441 - The optimizer exhausts an index, which means that the index has
445442 provided the full result set. At this point, the optimizer stops
@@ -449,8 +446,16 @@ To create a new query plan, the query optimizer:
449446 chooses the index that has provided the most results *first* and
450447 continues reading only from that index.
451448
452- The "winning" index becomes the index specified in the query plan:
453- future iterations of this query will use this index.
449+ The selected index becomes the index specified in the query plan;
450+ future iterations of this query or queries with the same query
451+ pattern will use this index. Query pattern refers to query select
452+ conditions that differ only in the values, as in the following two
453+ queries with the same query pattern:
454+
455+ .. code-block:: javascript
456+
457+ db.inventory.find( { type: 'food' } )
458+ db.inventory.find( { type: 'utensil' } )
454459
455460To manually compare the performance of a query using more than one
456461index, you can use the :method:`explain() <cursor.explain()>` method
@@ -459,15 +464,15 @@ following prototype:
459464
460465.. code-block:: javascript
461466
462- db.colection .find().explain().hint()
467+ db.collection .find().explain().hint()
463468
464469The following operations each run the same query but will reflect the
465470use of the different indexes:
466471
467472.. code-block:: javascript
468473
469- db.collection .find( { type: 'food' } ).explain().hint( { type: 1 } )
470- db.collection .find( { type: 'food' } ).explain().hint( { type: 1, name: 1 })
474+ db.inventory .find( { type: 'food' } ).explain().hint( { type: 1 } )
475+ db.inventory .find( { type: 'food' } ).explain().hint( { type: 1, name: 1 })
471476
472477This returns the statistics regarding the execution of the query. For
473478more information on the output of :method:`explain()
@@ -477,20 +482,20 @@ more information on the output of :method:`explain()
477482.. note::
478483
479484 If you run :method:`explain() <cursor.explain()>` without including
480- :method:`hint() <cursor.hint()>` the query optimizer re-evaluates
485+ :method:`hint() <cursor.hint()>`, the query optimizer re-evaluates
481486 the query and runs against multiple indexes before returning the query
482487 statistics.
483488
484489As collections change over time, the query optimizer deletes a query
485490plan and re-evaluates the after any of the following events:
486491
487- - the collection recieves 1,000 write operations.
492+ - the collection receives 1,000 write operations.
488493
489494- the :dbcommand:`reIndex` rebuilds the index.
490495
491496- the :program:`mongod` process restarts.
492497
493- The order that MongoDB returns documents depend on the index used to
498+ The order that MongoDB returns documents depends on the index used to
494499support the query. If you issue a query successive times and the data
495500hasn't changed, the query will return the same results in the same
496501order. However, in some cases MongoDB may return results in a
@@ -502,21 +507,21 @@ For more information, see :doc:`/applications/indexes`.
502507Query Operations that Cannot Use Indexes Effectively
503508~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
504509
505- Some query operations cannot use indexes effectively, or cannot use
510+ Some query operations cannot use indexes effectively or cannot use
506511indexes at all. Consider the following situations:
507512
508- - The inequality operators :operator:`$nin` and :operator:`$ne`, are
513+ - The inequality operators :operator:`$nin` and :operator:`$ne` are
509514 not very selective, as they often match a large portion of the
510515 index.
511516
512- As a result, in most cases a :operator:`$nin` or :operator:`$ne`
517+ As a result, in most cases, a :operator:`$nin` or :operator:`$ne`
513518 query with an index may perform no better than a :operator:`$nin` or
514519 :operator:`$ne` query that must scan all documents in a collection.
515520
516- - Queries that use specify regular expressions, with inline JavaScript
517- regular expressions or :operator:`$regex` operations cannot use an
518- index *unless* the regular expression is anchored to the beginning
519- or end of a string.
521+ - Queries that specify regular expressions, with inline JavaScript
522+ regular expressions or :operator:`$regex` operator expressions,
523+ cannot use an index *unless* the regular expression is anchored to
524+ the beginning or end of a string.
520525
521526.. _read-operations-aggregation:
522527
@@ -527,7 +532,7 @@ Aggregation
527532
528533MongoDB can perform some basic data aggregation operations on results
529534before returning data to the application. Running aggregation
530- operations on the database sides can be more efficient than running
535+ operations on the database side can be more efficient than running
531536them in the application layer and can reduce the amount of data
532537MongoDB needs to send to the application. These aggregation operations
533538include basic grouping, counting, and even processing data using a map
@@ -586,18 +591,18 @@ data set among a cluster of program:`mongod` in a way that is nearly
586591transparent to the application. See the :doc:`/sharding` section of
587592this manual for additional information about these deployments.
588593
589- For a sharded clusters, issue all operations to one of the
594+ For a sharded cluster, you issue all operations to one of the
590595:program:`mongos` instances associated with the
591596cluster. :program:`mongos` instances route operations to the
592597:program:`mongod` in the cluster and behave like :program:`mongod`
593598instances to the application. Read operations to a sharded collection
594- in a sharded are largely the same as operations to a :term:`replica
599+ in a sharded cluster are largely the same as operations to a :term:`replica
595600set` or :term:`standalone` instances. See the section on :ref:`Read
596601Operations in Sharded Clusters <sharding-read-operations>` for more
597602information.
598603
599- In sharded deployments, the :program:`mongos` instance routes routes
600- the queries from the clients to the :program:`mongod` instnaces that
604+ In sharded deployments, the :program:`mongos` instance routes
605+ the queries from the clients to the :program:`mongod` instances that
601606hold the data, using the cluster metadata stored in the :ref:`config
602607database <sharding-config-server>`.
603608
@@ -630,7 +635,7 @@ You can configure the :ref:`read preference mode
630635per-operation basis to allow reads from :term:`secondaries
631636<secondary>` for backup operations, or to allow reads during
632637:ref:`failover <replica-set-failover>` situations. If the majority of
633- database use are read operations and , then using read preferences to
638+ database use are read operations, then using read preferences to
634639distribute reads can improve read throughput.
635640
636641Read operations from secondary members of replica sets are not
0 commit comments