@@ -4,12 +4,9 @@ Read Operations
44
55.. default-domain:: mongodb
66
7- Read operations determine how MongoDB returns collection data when you issue a query.
8-
9- This document describes how MongoDB performs read operations and how
10- different factors affect the efficiency of reads.
11-
12- .. TODO for information about queries, see ???.
7+ Read operations determine how MongoDB returns collection data when you
8+ issue a query. This document describes how MongoDB performs read
9+ operations and how different factors affect the efficiency of reads.
1310
1411.. index:: read operation; query
1512.. index:: query; read operations
@@ -18,9 +15,7 @@ different factors affect the efficiency of reads.
1815Query Operations
1916----------------
2017
21- Queries retrieve data from your database collections. How a query
22- retrieves data is dependent on MongoDB read operations and on the
23- indexes you have created.
18+ Queries are the primary read operations in MongoDB.
2419
2520.. _read-operations-query-syntax:
2621
@@ -29,7 +24,7 @@ Query Syntax
2924
3025For a list of query operators, see :doc:`/reference/operators`.
3126
32- .. TODO see the yet-to-be created query operations doc
27+ .. TODO link to the yet-to-be created query operations doc
3328
3429.. _read-operations-indexing:
3530
@@ -78,8 +73,8 @@ documents that match the query criteria also match the entire query.
7873
7974 - The :doc:`/indexes` documentation, in particular :doc:`/applications/indexes`
8075 - :doc:`/reference/operators`
81- - :method:`find <db.collection.find()>`
82- - :method:`findOne`
76+ - :method:`find() <db.collection.find()>`
77+ - :method:`findOne() <findOne> `
8378
8479.. index:: query optimizer
8580.. _read-operations-query-optimization:
@@ -138,8 +133,8 @@ For details on the output, see :method:`explain() <cursor.explain()>`.
138133.. note::
139134
140135 If you run :method:`explain() <cursor.explain()>` without including
141- :method:`hint() <cursor.hint()>`, the query optimizer re-evaluates
142- the query and run against multiple indexes before returning the query
136+ :method:`hint() <cursor.hint()>` the query optimizer re-evaluates
137+ the query and runs against multiple indexes before returning the query
143138 statistics.
144139
145140Because your collections will likely change over time, the query
@@ -148,17 +143,15 @@ of the following occur:
148143
149144- The number of writes to the collection reaches 1,000.
150145
151- - You run the :dbcommand:`reIndex` command on the index.
146+ - You run the :dbcommand:`reIndex() <reIndex> ` command on the index.
152147
153148- You restart :program:`mongod`.
154149
155- When you re-evaluate a query, the optimizer displays the same results
156- (assuming no data has changed) but might display the results in a
157- different order. Similarly, the :method:`explain() <cursor.explain()>`
158- method and :method:`hint() <cursor.hint()>` methods might display
159- different statistics. This is because the optimizer retrieves the
160- results from several indexes at once during re-evaluation, and the order
161- in which results appear depends on the order of the indexes within the
150+ When you re-evaluate a query, the optimizer displays the same results if
151+ no data has changed, but the the results might appear in a different
152+ order. This is because the optimizer retrieves the results from several
153+ indexes at once during re-evaluation, and the order in which results
154+ appear depends on the order in which indexes are arranged in the
162155parallel query operation.
163156
164157.. _read-operations-projection:
@@ -171,7 +164,11 @@ documents. If you run a query *without* a projection, the query returns
171164all fields and values for matching documents. By narrowing the fields to
172165display, projections can minimize network and deserialization costs.
173166
174- You specify a projection in the second document in a query.
167+ You specify a projection in the second document in a query:
168+
169+ .. code-block:: javascript
170+
171+ db.foo.find( { <search values> } , { <projection> } )
175172
176173.. example::
177174
@@ -184,17 +181,33 @@ You specify a projection in the second document in a query.
184181 { "_id" : 102 , x : 2 , y : 17 , z : 22 }
185182 { "_id" : 103 , x : 3 , y : 18 , z : 23 }
186183
187- You can search for all documents where ``x`` is ``2`` but, instead of
188- returning all fields, you could choose to return only the ``z`` field
189- (and the ``_id`` field, which is always returned by default). You
190- would issue this query:
184+ To search for all documents where ``x`` is ``2`` but return only the
185+ ``z`` field, issue this query:
191186
192187 .. code-block:: javascript
193188
194189 db.foo.find( { x : 2 } , { z : 1 } )
195190
196- By specifying ``{ z : 1 }``, you tell MongoDB to return the ``z``
197- field.
191+ The query returns this result set:
192+
193+ .. code-block:: javascript
194+
195+ { "_id" : 101, "z" : 21 }
196+ { "_id" : 102, "z" : 22 }
197+
198+ By default, the results include the ``_id`` field. To return ``z``
199+ *without* returning ``_id``, include ``_id : 0`` in the projection:
200+
201+ .. code-block:: javascript
202+
203+ db.foo.find( { x : 2 } , { z : 1 , _id : 0 } )
204+
205+ The query returns this result set:
206+
207+ .. code-block:: javascript
208+
209+ { "z" : 21 }
210+ { "z" : 22 }
198211
199212MongoDB also provides the following projection operators specific to
200213arrays. For documentation on each operator, click the operator name:
@@ -208,13 +221,30 @@ arrays. For documentation on each operator, click the operator name:
208221Aggregation
209222~~~~~~~~~~~
210223
211- .. Probably short, but there's no docs for old-style aggregation so.
224+ When you run a query using aggregation, MongoDB performs summary,
225+ grouping, or other operations on data before returning results. When you
226+ run a query without aggregation, you retrieve data as it's stored in the
227+ database; when you run with aggregation, you retrieve reframed data.
228+
229+ Beginning with MongoDB version 2.1, the primary way to perform
230+ aggregation is through the aggregation framework, which processes data
231+ through pipelines and returns data as a document stream specific to the
232+ aggregation process.
233+
234+ For more information on the aggregation framework, including
235+ descriptions of operators, see see :doc:`/applications/aggregation`.
236+
237+ In addition to the operators used by the aggregation framework, you can
238+ use the following aggregation operators. For documentation on each
239+ operator, click the operator name:
240+
241+ - :method:`count() <cursor.count()>`
212242
213- .. - basic aggregation (count, distinct)
214- .. - legacy agg: group
215- .. - big things: mapreduce, aggregation
243+ - :dbcommand:`distinct() <distinct>`
216244
217- .. seealso:: :doc:`/applications/aggregation`
245+ - :method:`group()`
246+
247+ - :dbcommand:`mapReduce() <mapreduce>` (See also :wiki:`MapReduce`.)
218248
219249.. index:: read operation; architecture
220250.. _read-operations-architecture:
@@ -246,9 +276,31 @@ Architecture
246276Connection Pooling
247277~~~~~~~~~~~~~~~~~~
248278
279+ .. TODO
280+
249281Shard Clusters
250282~~~~~~~~~~~~~~
251283
284+ .. TODO
285+
252286Replica Sets
253287~~~~~~~~~~~~
254288
289+ :term:`Replica sets <replica set>` use :term:`read preferences <read
290+ preference>` to determine where and how to route read operations. By
291+ default, MongoDB always reads data from a repilca set's :term:`primary`.
292+ You can modify that behavior by changing the :ref:`read preference mode
293+ <replica-set-read-preference-modes>`.
294+
295+ For example, you can set the :ref:`read preference mode
296+ <replica-set-read-preference-modes>` to allow reads from
297+ :term:`secondaries <secondary>` when backing up data, or to block reads
298+ entirely during a :ref:`failover <replica-set-failover>`.
299+
300+ If your database traffic is comprised mostly of read operations, then
301+ using read preferences to distribute reads can improve read throughput.
302+ The trade-off is that some reads might return stale data, as
303+ secondaries always have some amount of lag.
304+
305+ For more information on choosing read preference modes, see
306+ :ref:`replica-set-read-preference-modes`.
0 commit comments