@@ -10,7 +10,7 @@ modifying data are read operations. In MongoDB, all read operations or
10
10
:term:`documents` that match the query criteria in a collection.
11
11
12
12
This 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
14
14
operations and how different factors affect the efficiency of reads.
15
15
16
16
.. note::
@@ -28,39 +28,38 @@ operations and how different factors affect the efficiency of reads.
28
28
Queries in MongoDB
29
29
------------------
30
30
31
- In the :program:`mongo` shell the :method:`find()
31
+ In the :program:`mongo` shell, the :method:`find()
32
32
<db.collection.find()>` and :method:`findOne()
33
33
<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
35
35
syntax:
36
36
37
37
.. code-block:: javascript
38
38
39
39
db.collection.find( <query>, <projection> )
40
40
41
- - The ``db.collection`` object, describes the database and collection
41
+ - The ``db.collection`` object specifies the database and collection
42
42
to query. All queries in MongoDB address a *single* collection.
43
43
44
44
You can enter ``db`` in the :program:`mongo` shell to return the
45
45
name of the current database. Use the ``show collections`` operation
46
46
in the :program:`mongo` shell to list the current collections in the
47
47
database.
48
48
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.
52
52
53
53
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
56
56
collection.
57
57
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.
61
60
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
64
63
larger, or when your application only needs a subset of available
65
64
fields.
66
65
@@ -94,7 +93,7 @@ operators, refer to the :doc:`/applications/read` page of the
94
93
Query Document
95
94
~~~~~~~~~~~~~~
96
95
97
- This section provides an overview of query document for MongoDB
96
+ This section provides an overview of the query document for MongoDB
98
97
queries. See the preceding section for more information on
99
98
:ref:`queries in MongoDB <read-operations-query-operations>`.
100
99
@@ -112,7 +111,7 @@ collection of documents named ``inventory``:
112
111
113
112
Not specifying a query document to the :method:`find()
114
113
<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
116
115
previous operation:
117
116
118
117
.. code-block:: javascript
@@ -123,26 +122,25 @@ collection of documents named ``inventory``:
123
122
field has a certain value. These are simple "equality" queries.
124
123
125
124
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' ``:
127
126
128
127
.. code-block:: javascript
129
128
130
129
db.inventory.find( { type: "snacks" } )
131
130
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'``:
137
135
138
136
.. code-block:: javascript
139
137
140
138
db.inventory.find( { tags: "fruit" } )
141
139
142
140
- 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.
146
144
147
145
In the following example, the query selects all documents in the
148
146
collection where the value of the ``type`` field is either
@@ -160,10 +158,9 @@ collection of documents named ``inventory``:
160
158
same field.
161
159
162
160
- 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.
167
164
168
165
In the following example, the query document specifies an equality
169
166
match on a single field, followed by a range of values for a second
@@ -174,13 +171,13 @@ collection of documents named ``inventory``:
174
171
db.inventory.find( { type: 'food', price: { $lt: 9.95 } } )
175
172
176
173
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
178
175
(i.e. :operator:`$lt`) ``9.95``.
179
176
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 .
184
181
185
182
In the following example, the query document selects all documents
186
183
in the collection where the field ``qty`` has a value greater than
@@ -216,9 +213,9 @@ documentation of all query operators.
216
213
Result Projections
217
214
~~~~~~~~~~~~~~~~~~
218
215
219
- The :term:`projection` specification, limits the fields to return for
216
+ The :term:`projection` specification limits the fields to return for
220
217
all 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
222
219
deserializing documents in the application layer.
223
220
224
221
The second argument to the :method:`find() <db.collection.find()>`
@@ -233,7 +230,7 @@ included, unless explicitly excluded.
233
230
You cannot combine inclusion and exclusion semantics in a single
234
231
projection with the *exception* of the ``_id`` field.
235
232
236
- Consider the following projection specifications, in :method:`find()
233
+ Consider the following projection specifications in :method:`find()
237
234
<db.collection.find()>` operations:
238
235
239
236
- If you specify no projection, the :method:`find()
@@ -245,12 +242,12 @@ Consider the following projection specifications, in :method:`find()
245
242
db.inventory.find( { type: 'food' } )
246
243
247
244
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' ``.
249
246
250
247
- A project can explicitly include several fields. In the following
251
248
operation, :method:`find() <db.collection.find()>` method returns
252
249
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 :
254
251
255
252
.. code-block:: javascript
256
253
@@ -274,7 +271,7 @@ Consider the following projection specifications, in :method:`find()
274
271
db.inventory.find( { type: 'food' }, { type:0 } )
275
272
276
273
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
278
275
output.
279
276
280
277
With the exception of the ``_id`` field you cannot combine inclusion
@@ -324,7 +321,7 @@ Measuring Index Use
324
321
~~~~~~~~~~~~~~~~~~~
325
322
326
323
The :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
328
325
analyzing the efficiency of queries, and for determining how the query
329
326
uses the index. Call the :method:`explain() <cursor.explain()>` method
330
327
on 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:
341
338
plans, it does not reflect accurate query performance.
342
339
343
340
If 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:
345
342
346
343
.. code-block:: javascript
347
344
@@ -414,7 +411,7 @@ the query used an index. This query:
414
411
the :data:`nscannedObjects` field.
415
412
416
413
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
418
415
:ref:`indexes-covered-queries` for more information.
419
416
420
417
.. index:: query optimizer
@@ -439,7 +436,7 @@ To create a new query plan, the query optimizer:
439
436
If an index returns a result already returned by another index, the
440
437
optimizer skips the duplicate match.
441
438
442
- #. determines a "winning" index when either of the following occur:
439
+ #. selects an index when either of the following occur:
443
440
444
441
- The optimizer exhausts an index, which means that the index has
445
442
provided the full result set. At this point, the optimizer stops
@@ -449,8 +446,16 @@ To create a new query plan, the query optimizer:
449
446
chooses the index that has provided the most results *first* and
450
447
continues reading only from that index.
451
448
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' } )
454
459
455
460
To manually compare the performance of a query using more than one
456
461
index, you can use the :method:`explain() <cursor.explain()>` method
@@ -459,15 +464,15 @@ following prototype:
459
464
460
465
.. code-block:: javascript
461
466
462
- db.colection .find().explain().hint()
467
+ db.collection .find().explain().hint()
463
468
464
469
The following operations each run the same query but will reflect the
465
470
use of the different indexes:
466
471
467
472
.. code-block:: javascript
468
473
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 })
471
476
472
477
This returns the statistics regarding the execution of the query. For
473
478
more information on the output of :method:`explain()
@@ -477,20 +482,20 @@ more information on the output of :method:`explain()
477
482
.. note::
478
483
479
484
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
481
486
the query and runs against multiple indexes before returning the query
482
487
statistics.
483
488
484
489
As collections change over time, the query optimizer deletes a query
485
490
plan and re-evaluates the after any of the following events:
486
491
487
- - the collection recieves 1,000 write operations.
492
+ - the collection receives 1,000 write operations.
488
493
489
494
- the :dbcommand:`reIndex` rebuilds the index.
490
495
491
496
- the :program:`mongod` process restarts.
492
497
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
494
499
support the query. If you issue a query successive times and the data
495
500
hasn't changed, the query will return the same results in the same
496
501
order. However, in some cases MongoDB may return results in a
@@ -502,21 +507,21 @@ For more information, see :doc:`/applications/indexes`.
502
507
Query Operations that Cannot Use Indexes Effectively
503
508
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
504
509
505
- Some query operations cannot use indexes effectively, or cannot use
510
+ Some query operations cannot use indexes effectively or cannot use
506
511
indexes at all. Consider the following situations:
507
512
508
- - The inequality operators :operator:`$nin` and :operator:`$ne`, are
513
+ - The inequality operators :operator:`$nin` and :operator:`$ne` are
509
514
not very selective, as they often match a large portion of the
510
515
index.
511
516
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`
513
518
query with an index may perform no better than a :operator:`$nin` or
514
519
:operator:`$ne` query that must scan all documents in a collection.
515
520
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.
520
525
521
526
.. _read-operations-aggregation:
522
527
@@ -527,7 +532,7 @@ Aggregation
527
532
528
533
MongoDB can perform some basic data aggregation operations on results
529
534
before 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
531
536
them in the application layer and can reduce the amount of data
532
537
MongoDB needs to send to the application. These aggregation operations
533
538
include 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
586
591
transparent to the application. See the :doc:`/sharding` section of
587
592
this manual for additional information about these deployments.
588
593
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
590
595
:program:`mongos` instances associated with the
591
596
cluster. :program:`mongos` instances route operations to the
592
597
:program:`mongod` in the cluster and behave like :program:`mongod`
593
598
instances 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
595
600
set` or :term:`standalone` instances. See the section on :ref:`Read
596
601
Operations in Sharded Clusters <sharding-read-operations>` for more
597
602
information.
598
603
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
601
606
hold the data, using the cluster metadata stored in the :ref:`config
602
607
database <sharding-config-server>`.
603
608
@@ -630,7 +635,7 @@ You can configure the :ref:`read preference mode
630
635
per-operation basis to allow reads from :term:`secondaries
631
636
<secondary>` for backup operations, or to allow reads during
632
637
: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
634
639
distribute reads can improve read throughput.
635
640
636
641
Read operations from secondary members of replica sets are not
0 commit comments