@@ -4,12 +4,9 @@ Read Operations
4
4
5
5
.. default-domain:: mongodb
6
6
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.
13
10
14
11
.. index:: read operation; query
15
12
.. index:: query; read operations
@@ -18,9 +15,7 @@ different factors affect the efficiency of reads.
18
15
Query Operations
19
16
----------------
20
17
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.
24
19
25
20
.. _read-operations-query-syntax:
26
21
@@ -29,7 +24,7 @@ Query Syntax
29
24
30
25
For a list of query operators, see :doc:`/reference/operators`.
31
26
32
- .. TODO see the yet-to-be created query operations doc
27
+ .. TODO link to the yet-to-be created query operations doc
33
28
34
29
.. _read-operations-indexing:
35
30
@@ -78,8 +73,8 @@ documents that match the query criteria also match the entire query.
78
73
79
74
- The :doc:`/indexes` documentation, in particular :doc:`/applications/indexes`
80
75
- :doc:`/reference/operators`
81
- - :method:`find <db.collection.find()>`
82
- - :method:`findOne`
76
+ - :method:`find() <db.collection.find()>`
77
+ - :method:`findOne() <findOne> `
83
78
84
79
.. index:: query optimizer
85
80
.. _read-operations-query-optimization:
@@ -138,8 +133,8 @@ For details on the output, see :method:`explain() <cursor.explain()>`.
138
133
.. note::
139
134
140
135
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
143
138
statistics.
144
139
145
140
Because your collections will likely change over time, the query
@@ -148,17 +143,15 @@ of the following occur:
148
143
149
144
- The number of writes to the collection reaches 1,000.
150
145
151
- - You run the :dbcommand:`reIndex` command on the index.
146
+ - You run the :dbcommand:`reIndex() <reIndex> ` command on the index.
152
147
153
148
- You restart :program:`mongod`.
154
149
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
162
155
parallel query operation.
163
156
164
157
.. _read-operations-projection:
@@ -171,7 +164,11 @@ documents. If you run a query *without* a projection, the query returns
171
164
all fields and values for matching documents. By narrowing the fields to
172
165
display, projections can minimize network and deserialization costs.
173
166
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> } )
175
172
176
173
.. example::
177
174
@@ -184,17 +181,33 @@ You specify a projection in the second document in a query.
184
181
{ "_id" : 102 , x : 2 , y : 17 , z : 22 }
185
182
{ "_id" : 103 , x : 3 , y : 18 , z : 23 }
186
183
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:
191
186
192
187
.. code-block:: javascript
193
188
194
189
db.foo.find( { x : 2 } , { z : 1 } )
195
190
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 }
198
211
199
212
MongoDB also provides the following projection operators specific to
200
213
arrays. For documentation on each operator, click the operator name:
@@ -208,13 +221,30 @@ arrays. For documentation on each operator, click the operator name:
208
221
Aggregation
209
222
~~~~~~~~~~~
210
223
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()>`
212
242
213
- .. - basic aggregation (count, distinct)
214
- .. - legacy agg: group
215
- .. - big things: mapreduce, aggregation
243
+ - :dbcommand:`distinct() <distinct>`
216
244
217
- .. seealso:: :doc:`/applications/aggregation`
245
+ - :method:`group()`
246
+
247
+ - :dbcommand:`mapReduce() <mapreduce>` (See also :wiki:`MapReduce`.)
218
248
219
249
.. index:: read operation; architecture
220
250
.. _read-operations-architecture:
@@ -246,9 +276,31 @@ Architecture
246
276
Connection Pooling
247
277
~~~~~~~~~~~~~~~~~~
248
278
279
+ .. TODO
280
+
249
281
Shard Clusters
250
282
~~~~~~~~~~~~~~
251
283
284
+ .. TODO
285
+
252
286
Replica Sets
253
287
~~~~~~~~~~~~
254
288
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