@@ -54,6 +54,12 @@ and ``price`` fields of the ``products`` collection:
54
54
55
55
db.products.ensureIndex( { item: 1, category: 1, price: 1 } )
56
56
57
+
58
+ .. note::
59
+
60
+ To build indexes for a :term:`replica set`, before version 2.2,
61
+ see :ref:`index-building-replica-sets`.
62
+
57
63
.. [#ensure] As the name suggests, :func:`ensureIndex() <db.collection.ensureIndex()>`
58
64
only creates an index if an index of the same specification does
59
65
not already exist.
@@ -128,18 +134,74 @@ and *not* for either key individually.
128
134
Removal
129
135
~~~~~~~
130
136
131
- :func:`db.collection.dropIndex()`
137
+ To remove an index, use the :func:`db.collection.dropIndex()` method,
138
+ as in the following example:
139
+
140
+ .. code-block:: javascript
141
+
142
+ db.accounts.dropIndex( { tax-id: 1 } )
143
+
144
+ This will remove the index on the ``tax-id`` field in the ``accounts``
145
+ collection. The shell provides the following document after completing
146
+ the operation:
147
+
148
+ .. code-block:: javascript
149
+
150
+ { "nIndexesWas" : 3, "ok" : 1 }
132
151
133
- :func:`db.collection.dropIndexes()`
152
+ Where the value of ``nIndexesWas`` reflects the number of indexes
153
+ *before* removing this index. You can also use the
154
+ :func:`db.collection.dropIndexes()` to remove *all* indexes, except
155
+ for the :ref:`_id index <index-type-primary>` from a collection.
134
156
135
- :dbcommand:`deleteIndexes`
157
+ These shell helpers provide wrappers around the
158
+ :dbcommand:`deleteIndexes` :term:`database command`. Your :ref:`client
159
+ library </applications/drivers>` may have a different or additional
160
+ interface for these operations.
136
161
137
162
Rebuilding
138
163
~~~~~~~~~~
139
164
140
- :func:`db.collection.reIndex()`
165
+ If you need to rebuild indexes for a collection you can use the
166
+ :func:`db.collection.reIndex()` method. This will drop all indexes,
167
+ including the :ref:`_id index <index-type-primary>`, and then rebuild
168
+ all indexes. For examples:
141
169
142
- :dbcommand:`reIndex`
170
+ .. code-block:: javascript
171
+
172
+ db.accounts.reIndex()
173
+
174
+ MongoDB will return the following document when the operation
175
+ completes:
176
+
177
+ .. code-block:: javascript
178
+
179
+ {
180
+ "nIndexesWas" : 2,
181
+ "msg" : "indexes dropped for collection",
182
+ "nIndexes" : 2,
183
+ "indexes" : [
184
+ {
185
+ "key" : {
186
+ "_id" : 1,
187
+ "tax-id" : 1
188
+ },
189
+ "ns" : "records.accounts",
190
+ "name" : "_id_"
191
+ }
192
+ ],
193
+ "ok" : 1
194
+ }
195
+
196
+ This shell helper provides a wrapper around the
197
+ :dbcommand:`reIndex` :term:`database command`. Your :ref:`client
198
+ library </applications/drivers>` may have a different or additional
199
+ interface for this operation.
200
+
201
+ .. note::
202
+
203
+ To rebuild indexes for a :term:`replica set`, before version 2.2,
204
+ see :ref:`index-rebuilding-replica-sets`.
143
205
144
206
Special Creation Options
145
207
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -150,28 +212,86 @@ Special Creation Options
150
212
Background
151
213
``````````
152
214
215
+ To create an index in the background you can specify :ref:`background
216
+ construction <index-creation-background>`. Consider the following
217
+ prototype invocation of :func:`db.collection.ensureIndex()`:
218
+
153
219
.. code-block:: javascript
154
220
155
221
db.collection.ensureIndex( { a: 1 }, { background: true } )
156
222
157
223
Drop Duplicates
158
224
```````````````
159
225
226
+ To force the creation of a :ref:`unique index <index-type-unique>`
227
+ Inez, you can use the ``dropDups`` option. This will force MongoDB to
228
+ create a unique index by deleting documents with duplicate
229
+ values. Consider the following prototype invocation of
230
+ :func:`db.collection.ensureIndex()`:
231
+
160
232
.. code-block:: javascript
161
233
162
234
db.collection.ensureIndex( { a: 1 }, { dropDups: true } )
163
235
164
- Rebuilding Indexes Replica Set
165
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
236
+ See the full documentation of :ref:`duplicate dropping
237
+ <index-creation-duplicate-dropping>` for more information.
238
+
239
+ .. warning::
240
+
241
+ Specifying ``{ dropDups: true }`` will delete data from your
242
+ database. Use with extreme caution.
243
+
244
+ .. _index-building-replica-sets:
245
+
246
+ Building Indexes Replica Set
247
+ ----------------------------
166
248
167
249
In Version 1.8 and 2.0
168
- ``````````````````````
250
+ ~~~~~~~~~~~~~~~~~~~~~~
251
+
252
+ For Version 1.8 and 2.0, :ref:`background index creation operations <index-creation-background>`
253
+ become *foreground* indexing operations on :term:`secondary` members
254
+ of replica sets. Because this can block replication on the
255
+ secondaries, especially with long running operations use the following
256
+ procedure for all non-trivial index builds:
257
+
258
+ #. Stop the :program:`mongod` process on one secondary. Restart the
259
+ :program:`mongod` process *without* the
260
+ :option:`--replSet <mongod --replSet>` option. This instance is
261
+ now in "standalone" mode.
262
+
263
+ #. Create the new index or rebuild the index on this :program:`mongod`
264
+ instance.
265
+
266
+ #. Restart the :program:`mongod` instance with the
267
+ :option:`--replSet <mongod --replSet>` option. Allow replication to
268
+ catch up on this node.
269
+
270
+ #. Replete this operation on all of the remaining secondaries.
271
+
272
+ #. Run :func:`rs.stepDown()` on the :term:`primary` member of the set,
273
+ and then run this procedure on the former primary.
274
+
275
+ .. warning::
276
+
277
+ Ensure that your :ref:`oplog` is large enough to permit the
278
+ indexing or re-indexing operation to complete without falling too
279
+ far behind to catch up. See the ":ref:`replica-set-oplog-sizing`"
280
+ documentation for additional information.
281
+
282
+ .. note::
283
+
284
+ This procedure *does* block indexing on one member of the replica
285
+ set at a time. However, the foreground indexing operation is more
286
+ efficient than the background index operation, and will only affect
287
+ one secondary at a time rather than *all* secondaries at the same
288
+ time.
169
289
170
290
.. rebuild indexes on secondaries and cycle so that there's never a
171
291
failover.
172
292
173
293
In Version 2.2
174
- ``````````````
294
+ ~~~~~~~~~~~~~~
175
295
176
296
.. versionchanged:: 2.2
177
297
@@ -181,10 +301,34 @@ Rebuild operation on :term:`secondary` members of :term:`replica sets
181
301
``{ background: true }`` option for replica sets.
182
302
183
303
Measuring Index Utilization
184
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
304
+ ---------------------------
305
+
306
+ .. TODO expand the .explain and .hint documentation here.
307
+
308
+ Query performance is a good general indicator of index utilization;
309
+ however, for more precise insight into index use, MongoDB provides the
310
+ following tools:
185
311
186
- - :func:`cursor.explain()`
312
+ - :func:`explain() <cursor.explain()>`
313
+
314
+ Append the :func:`explain() <cursor.explain()>` method to any cursor
315
+ (e.g. a query) to return a document with statistics about the query
316
+ process, including the index used, and the number of documents
317
+ scanned.
187
318
188
319
- :func:`cursor.hint()`
189
320
321
+ Append the :func:`hint() <cursor.explain()>` to any cursor (e.g. a
322
+ query) with the name of an index as the argument to *force* MongoDB
323
+ to use a specific index to fulfill the query. Consider the following
324
+ example:
325
+
326
+ .. code-block:: javascript
327
+
328
+ db.people.find( { name: "John Doe", zipcode: { $gt: 63000 } } } ).hint( { zipcode: 1 } )
329
+
190
330
- :status:`indexCounters`
331
+
332
+ Use the :status:`indexCounters` data in the output of
333
+ :dbcommand:`serverStatus` for insight into database-wise index
334
+ utilization.
0 commit comments