@@ -38,9 +38,7 @@ of the ``people`` collection:
38
38
39
39
.. code-block:: javascript
40
40
41
- db.people.ensureIndex( { phone-number: 1 } )
42
-
43
- TODO: you need ""s around phone-number, otherwise it's invalid JS (phone minus number).
41
+ db.people.ensureIndex( { "phone-number": 1 } )
44
42
45
43
To create a :ref:`compound index <index-type-compound>`, use an
46
44
operation that resembles the following prototype:
@@ -57,21 +55,18 @@ collection:
57
55
58
56
db.products.ensureIndex( { item: 1, category: 1, price: 1 } )
59
57
60
- .. note::
61
-
62
- To build indexes for a :term:`replica set`, before version 2.2,
63
- see :ref:`index-building-replica-sets`.
58
+ Some drivers may specify indexes, using ``NumberLong(1)`` rather than
59
+ ``1`` as the specification. This does not have any affect on the
60
+ resulting index.
64
61
65
- TODO: I don't think anything changed about replica set index builds for 2.2...
62
+ .. include:: /includes/note-build-indexes-on- replica-sets.rst
66
63
67
64
.. [#ensure] As the name suggests, :func:`ensureIndex() <db.collection.ensureIndex()>`
68
65
only creates an index if an index of the same specification does
69
66
not already exist.
70
67
71
- Sparse
72
- ``````
73
-
74
- TODO: Sparse? Maybe "Types of Indexes->Sparse"?
68
+ Sparse Indexes
69
+ ``````````````
75
70
76
71
To create a :ref:`sparse index <index-type-sparse>` on a field, use an
77
72
operation that resembles the following prototype:
@@ -91,16 +86,13 @@ without the ``twitter_name`` field.
91
86
92
87
.. note::
93
88
94
- MongoDB cannot create sparse compound indexes.
89
+ Sparse indexes can affect the results returned by the query,
90
+ particularly with respect to sorts on fields *not* included in the
91
+ index. See the :ref:`sparse index <index-type-sparse>` section for
92
+ more information.
95
93
96
- TODO: is this true? I thought that it could.
97
-
98
- TODO: Is there more doc on spare indexes somewhere? Seems like this is missing
99
- some info like getting different results back when the index is used, null
100
- counts as existing, etc.
101
-
102
- Unique
103
- ``````
94
+ Unique Indexes
95
+ ``````````````
104
96
105
97
To create a :ref:`unique indexes <index-type-unique>`, consider the
106
98
following prototype:
@@ -109,21 +101,17 @@ following prototype:
109
101
110
102
db.collection.ensureIndex( { a: 1 }, { unique: true } )
111
103
112
- For example, you may want to create a unique index on the ``tax-id:``
104
+ For example, you may want to create a unique index on the ``" tax-id" :``
113
105
of the ``accounts`` collection to prevent storing multiple account
114
106
records for the same legal entity:
115
107
116
108
.. code-block:: javascript
117
109
118
- db.accounts.ensureIndex( { tax-id: 1 }, { unique: true } )
119
-
120
- TODO: tax-id should be in ""s.
110
+ db.accounts.ensureIndex( { "tax-id": 1 }, { unique: true } )
121
111
122
112
The :ref:`_id index <index-type-primary>` is a unique index. In some
123
- situations you may want to use the ``_id`` field for these primary
124
- data rather than using a unique index on another field.
125
-
126
- TODO: "for these primary data"?
113
+ situations you may consider using ``_id`` field itself for this kind
114
+ of data rather than using a unique index on another field.
127
115
128
116
In many situations you will want to combine the ``unique`` constraint
129
117
with the ``sparse`` option. When MongoDB indexes a field, if a
@@ -155,11 +143,9 @@ as in the following example:
155
143
156
144
.. code-block:: javascript
157
145
158
- db.accounts.dropIndex( { tax-id: 1 } )
146
+ db.accounts.dropIndex( { " tax-id" : 1 } )
159
147
160
- TODO: ""s!
161
-
162
- This will remove the index on the ``tax-id`` field in the ``accounts``
148
+ This will remove the index on the ``"tax-id"`` field in the ``accounts``
163
149
collection. The shell provides the following document after completing
164
150
the operation:
165
151
@@ -216,16 +202,7 @@ This shell helper provides a wrapper around the :dbcommand:`reIndex`
216
202
</applications/drivers>` may have a different or additional interface
217
203
for this operation.
218
204
219
- .. note::
220
-
221
- To rebuild indexes for a :term:`replica set`, before version 2.2,
222
- see :ref:`index-rebuilding-replica-sets`.
223
-
224
- TODO: again, this probably isn't different in 2.2
225
-
226
- TODO: one thing that I would appreciate you mentioning is that some drivers may
227
- create indexes like {a : NumberLong(1)} _which is fine_ and doesn't break
228
- anything so stop complaining about it.
205
+ .. include:: /includes/note-build-indexes-on-replica-sets.rst
229
206
230
207
Special Creation Options
231
208
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -235,7 +212,8 @@ Special Creation Options
235
212
TTL collections use a special ``expire`` index option. See
236
213
:doc:`/tutorial/expire-data` for more information.
237
214
238
- TODO: Are 2d indexes getting a mention?
215
+ .. TODO: insert link here to the geospatial index documents when
216
+ they're published.
239
217
240
218
Background
241
219
``````````
@@ -248,26 +226,17 @@ prototype invocation of :func:`db.collection.ensureIndex()`:
248
226
249
227
db.collection.ensureIndex( { a: 1 }, { background: true } )
250
228
251
- TODO: what does it mean to build an index in the background? You might want to
252
- mention:
253
- * performance implications
254
- * that this type of index build can be killed
255
- * that this blocks the connection you sent the ensureindex on, but ops from
256
- other connections can proceed in
257
- * that indexes are created on the foreground on secondaries in 2.0,
258
- which blocks replication & slave reads. In 2.2, it does not block reads (but
259
- still blocks repl).
229
+ Consider the section on :ref:`background index construction
230
+ <index-creation-background>` for more information about these indexes
231
+ and their implications.
260
232
261
233
Drop Duplicates
262
234
```````````````
263
235
264
236
To force the creation of a :ref:`unique index <index-type-unique>`
265
- index
266
-
267
- TODO: " on a collection with duplicate values in the field to be indexed "
268
-
269
- you can use the ``dropDups`` option. This will force MongoDB to
270
- create a *unique* index by deleting documents with duplicate values
237
+ index on a collection with duplicate values in the field you are
238
+ indexing you can use the ``dropDups`` option. This will force MongoDB
239
+ to create a *unique* index by deleting documents with duplicate values
271
240
when building the index. Consider the following prototype invocation
272
241
of :func:`db.collection.ensureIndex()`:
273
242
@@ -280,82 +249,63 @@ See the full documentation of :ref:`duplicate dropping
280
249
281
250
.. warning::
282
251
283
- Specifying ``{ dropDups: true }`` will delete data from your
252
+ Specifying ``{ dropDups: true }`` may delete data from your
284
253
database. Use with extreme caution.
285
254
286
- TODO: I'd say it "may" delete data from your DB, not like it's going to go all
287
- Shermanesque on your data.
288
-
289
255
.. _index-building-replica-sets:
290
256
291
257
Building Indexes on Replica Sets
292
258
--------------------------------
293
259
294
- .. versionchanged:: 2.2
295
- Index rebuilding operations on :term:`secondary` members of
296
- :term:`replica sets <replica set>` now run as normal background
297
- index operations. Run :func:`ensureIndex()
298
- <db.collection.ensureIndex()>` normally with the ``{ background:
299
- true }`` option for replica sets. Alternatively, you may always use
300
- the following operation to isolate and control the impact of
301
- indexing building operations on a set as a whole.
302
-
303
- TODO: I think there needs to be a huge mention that this still blocks
304
- replication, so the procedure below is recommended.
305
-
306
- .. admonition:: For Version 1.8 and 2.0
307
-
308
- :ref:`Background index creation operations
309
- <index-creation-background>` became *foreground* indexing
310
- operations on :term:`secondary` members of replica sets. These
311
- foreground operations will block all replication on the
312
- secondaries,
260
+ Consideration
261
+ ~~~~~~~~~~~~~
313
262
314
- TODO: and don't allow any reads to go through.
263
+ :ref:`Background index creation operations
264
+ <index-creation-background>` become *foreground* indexing operations
265
+ on :term:`secondary` members of replica sets. These foreground
266
+ operations will block all replication on the secondaries, and don't
267
+ allow any reads. As a result in most cases use the following procedure
268
+ to build indexes on secondaries.
315
269
316
- and can impact performance of the entire set. To build
317
- indexes with minimal impact on a replica set, use the following
318
- procedure for all non-trivial index builds:
270
+ Procedure
271
+ ~~~~~~~~~
319
272
320
- #. Stop the :program:`mongod` process on one secondary. Restart the
321
- :program:`mongod` process *without* the :option:`--replSet <mongod --replSet>`
322
- option. This instance is now in "standalone" mode.
273
+ #. Stop the :program:`mongod` process on one secondary. Restart the
274
+ :program:`mongod` process *without* the :option:`--replSet <mongod --replSet>`
275
+ option and running on a different port. [#different-port]_ This
276
+ instance is now in "standalone" mode.
323
277
324
- TODO: generally we recommend running it on a different port, too, so that apps
325
- & other servers in the set don't try to contact it .
278
+ #. Create the new index or rebuild the index on this :program:`mongod`
279
+ instance .
326
280
327
- #. Create the new index or rebuild the index on this :program:`mongod`
328
- instance.
281
+ #. Restart the :program:`mongod` instance with the
282
+ :option:`--replSet <mongod --replSet>` option. Allow replication
283
+ to catch up on this member.
329
284
330
- #. Restart the :program:`mongod` instance with the
331
- :option:`--replSet <mongod --replSet>` option. Allow replication
332
- to catch up on this member.
285
+ #. Repeat this operation on all of the remaining secondaries.
333
286
334
- #. Replete this operation on all of the remaining secondaries.
287
+ #. Run :func:`rs.stepDown()` on the :term:`primary` member of the
288
+ set, and then repeat this procedure on the former primary.
335
289
336
- #. Run :func:`rs.stepDown()` on the :term:`primary` member of the
337
- set, and then run this procedure on the former primary.
338
-
339
- .. warning::
290
+ .. warning::
340
291
341
- Ensure that your :ref:`oplog` is large enough to permit the
342
- indexing or re-indexing operation to complete without falling
343
- too far behind to catch up. See the ":ref:`replica-set-oplog-sizing`"
344
- documentation for additional information.
292
+ Ensure that your :ref:`oplog` is large enough to permit the
293
+ indexing or re-indexing operation to complete without falling
294
+ too far behind to catch up. See the ":ref:`replica-set-oplog-sizing`"
295
+ documentation for additional information.
345
296
346
- .. note::
297
+ .. note::
347
298
348
- This procedure *does* block indexing on one member of the
349
- replica set at a time. However, the foreground indexing
350
- operation is more efficient than the background index operation,
351
- and will only affect one secondary at a time rather than *all*
352
- secondaries at the same time.
299
+ This procedure *does* take one member out of the replica set at a
300
+ time. However, this procedure will only affect one member of the
301
+ set at a time rather than *all* secondaries at the same time.
353
302
354
- For the best results, always create indexes *before* you begin
355
- inserting data into a collection.
303
+ .. [#different-port] By running the :program:`mongod` on a different
304
+ port, you ensure that the other members of the replica set and all
305
+ clients will not contact the member while you are building the
306
+ index.
356
307
357
- TODO: well, sort of. That'll build the indexes fast, but make the inserts
358
- slower. Overall, it's faster to insert data, then build indexes.
308
+ .. _indexes-measuring-use:
359
309
360
310
Measuring Index Use
361
311
-------------------
@@ -374,31 +324,41 @@ following tools:
374
324
- :func:`cursor.hint()`
375
325
376
326
Append the :func:`hint() <cursor.hint()>` to any cursor (e.g.
377
- query) with the name
378
-
379
- TODO: this isn't "the name of an index." I'd say just "with the index." The
380
- name of an index is a string like "zipcode_1".
381
-
382
- of an index as the argument to *force* MongoDB
327
+ query) with the index as the argument to *force* MongoDB
383
328
to use a specific index to fulfill the query. Consider the following
384
329
example:
385
330
386
331
.. code-block:: javascript
387
332
388
333
db.people.find( { name: "John Doe", zipcode: { $gt: 63000 } } } ).hint( { zipcode: 1 } )
389
334
390
-
391
335
You can use :func:`hint() <cursor.hint()>` and :func:`explain()
392
336
<cursor.explain()>` in conjunction with each other to compare the
393
- effectiveness of a specific index.
337
+ effectiveness of a specific index. Specify the ``$natural`` operator
338
+ to the :func:`hint() <cursor.hint()>` method to prevent MongoDB from
339
+ using *any* index:
340
+
341
+ .. code-block:: javascript
394
342
395
- TODO: mention $natural to force no index usage?
343
+ db.people.find( { name: "John Doe", zipcode: { $gt: 63000 } } } ).hint( { $natural: 1 } )
396
344
397
345
- :status:`indexCounters`
398
346
399
347
Use the :status:`indexCounters` data in the output of
400
348
:dbcommand:`serverStatus` for insight into database-wise index
401
349
utilization.
402
350
403
- TODO: I'd like to see this also cover how to track how far an index build has
404
- gotten and how to kill an index build.
351
+ Monitoring and Controlling Index Building
352
+ -----------------------------------------
353
+
354
+ .. TODO insert links to the values in the inprog array following the
355
+ completion of DOCS-162
356
+
357
+ To see the status of the indexing processes, you can use the
358
+ :func:`db.currentOp()` method in the :program:`mongo` shell. The value
359
+ of the ``query`` field and the ``msg`` field will indicate if the
360
+ operation is an index build. The ``msg`` field also indicates the
361
+ percent of the build that is complete.
362
+
363
+ If you need to terminate an ongoing index build, You can use the
364
+ :func:`db.killOP()` method in the :program:`mongo` shell.
0 commit comments