-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
db.collection.createIndex.txt
474 lines (265 loc) · 11.7 KB
/
db.collection.createIndex.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
===========================
db.collection.createIndex()
===========================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.collection.createIndex(keys, options)
Creates indexes on collections.
.. versionchanged:: 3.2
Starting in MongoDB 3.2, MongoDB disallows the creation of
:ref:`version 0 <3.2-version-0-indexes>` indexes. To upgrade
existing version 0 indexes, see :ref:`3.2-version-0-indexes`.
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``keys``
- document
- A document that contains the field and value pairs where the field is
the index key and the value describes the type of index for that
field. For an ascending index on a field, specify a value of ``1``; for
descending index, specify a value of ``-1``.
MongoDB supports several different index types including
:ref:`text <index-feature-text>`, :ref:`geospatial
<index-feature-geospatial>`, and :ref:`hashed
<index-type-hashed>` indexes. See :ref:`index types <index-types>`
for more information.
* - ``options``
- document
- Optional. A document that contains a set of options that controls the creation
of the index. See :ref:`ensureIndex-options` for details.
.. _ensureIndex-options:
.. _createIndex-options:
Options
-------
The ``options`` document contains a set of options that controls the
creation of the index. Different index types can have additional
options specific for that type.
Options for All Index Types
~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following options are available for all index types unless
otherwise specified:
.. include:: /includes/fact-remove-dropDups-option.rst
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``background``
- boolean
- Optional. Builds the index in the background so the operation does
*not* block other database activities. Specify ``true`` to build in
the background. The default value is ``false``.
* - ``unique``
- boolean
- Optional. Creates a unique index so that the collection will not accept
insertion or update of documents where the index key value matches an existing
value in the index.
Specify ``true`` to create a unique index. The
default value is ``false``.
The option is *unavailable* for :doc:`hashed </core/index-hashed>`
indexes.
* - ``name``
- string
- Optional. The name of the index. If unspecified, MongoDB generates an index name
by concatenating the names of the indexed fields and the sort order.
Whether user specified or MongoDB generated, index names including
their full namespace (i.e. ``database.collection``) cannot be longer
than the :limit:`Index Name Limit <Index Name Length>`.
* - ``partialFilterExpression``
- document
- Optional. If specified, the index only references documents that match the
filter expression. See :doc:`/core/index-partial` for more information.
A filter expression can include:
.. include:: /includes/fact-partial-filter-expression-operators.rst
You can specify a ``partialFilterExpression`` option for all MongoDB
:ref:`index types <index-types>`.
.. versionadded:: 3.2
* - ``sparse``
- boolean
- Optional. If ``true``, the index only references documents with the specified
field. These indexes use less space but behave differently in some
situations (particularly sorts). The default value is ``false``.
See :doc:`/core/index-sparse` for more information.
.. versionchanged:: 3.2
Starting in MongoDB 3.2, MongoDB provides the option to create
:ref:`partial indexes <index-type-partial>`. Partial indexes
offer a superset of the functionality of sparse indexes. If you
are using MongoDB 3.2 or later, :ref:`partial indexes
<index-type-partial>` should be preferred over sparse indexes.
.. versionchanged:: 2.6
:doc:`2dsphere </core/2dsphere>` indexes are sparse by default and
ignore this option. For a compound index that includes
``2dsphere`` index key(s) along with keys of other types, only the
``2dsphere`` index fields determine whether the index references a
document.
:doc:`2d </core/2d>`, :doc:`geoHaystack </core/geohaystack>`, and
:doc:`text </core/index-text>` indexes behave similarly to the
:doc:`2dsphere </core/2dsphere>` indexes.
* - ``expireAfterSeconds``
- integer
- Optional. Specifies a value, in seconds, as a :term:`TTL` to control how long
MongoDB retains documents in this collection. See
:doc:`/tutorial/expire-data` for more information on this
functionality. This applies only to :term:`TTL` indexes.
* - ``storageEngine``
- document
- Optional. Allows users to configure the storage engine on a
per-index basis when creating an index.
The ``storageEngine`` option should take the following form:
.. code-block:: javascript
storageEngine: { <storage-engine-name>: <options> }
Storage engine configuration options specified when creating indexes are
validated and logged to the :term:`oplog` during replication to
support replica sets with members that use different storage
engines.
.. versionadded:: 3.0
Options for ``text`` Indexes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following options are available for :doc:`text </core/index-text>`
indexes only:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``weights``
- document
- Optional. For :doc:`text </core/index-text>` indexes, a document that contains
field and weight pairs. The weight is an integer ranging from 1 to
99,999 and denotes the significance of the field relative to the
other indexed fields in terms of the score. You can specify weights
for some or all the indexed fields. See
:doc:`/tutorial/control-results-of-text-search` to adjust the scores.
The default value is ``1``.
* - ``default_language``
- string
- Optional. For :doc:`text </core/index-text>` indexes, the language that
determines the list of stop words and the rules for the stemmer and
tokenizer. See :ref:`text-search-languages` for the available
languages and :doc:`/tutorial/specify-language-for-text-index` for
more information and examples. The default value is ``english``.
* - ``language_override``
- string
- Optional. For :doc:`text </core/index-text>` indexes, the name of the field, in
the collection's documents, that contains the override language for
the document. The default value is ``language``. See
:ref:`specify-language-field-text-index-example` for an example.
* - ``textIndexVersion``
- integer
- Optional. The ``text`` index version number. Users can use this option to
override the default version number.
For available versions, see :ref:`text-versions`.
.. versionadded:: 2.6
Options for ``2dsphere`` Indexes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following option is available for :doc:`2dsphere </core/2dsphere>`
indexes only:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``2dsphereIndexVersion``
- integer
- Optional. The ``2dsphere`` index version number. Users can use this option to
override the default version number.
For the available versions, see :ref:`2dsphere-v2`.
.. versionadded:: 2.6
Options for ``2d`` Indexes
~~~~~~~~~~~~~~~~~~~~~~~~~~
The following options are available for :doc:`2d </core/2d>` indexes
only:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``bits``
- integer
- Optional. For :doc:`2d </core/2d>` indexes, the number of precision of the
stored :term:`geohash` value of the location data.
The ``bits`` value ranges from 1 to 32 inclusive. The default value
is ``26``.
* - ``min``
- number
- Optional. For :doc:`2d </core/2d>` indexes, the lower inclusive boundary for
the longitude and latitude values. The default value is ``-180.0``.
* - ``max``
- number
- Optional. For :doc:`2d </core/2d>` indexes, the upper inclusive boundary for
the longitude and latitude values. The default value is ``180.0``.
Options for ``geoHaystack`` Indexes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following option is available for :doc:`geoHaystack </core/geohaystack>`
indexes only:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``bucketSize``
- number
- For :doc:`geoHaystack </core/geohaystack>` indexes, specify the
number of units within which to group the location values; i.e. group
in the same bucket those location values that are within the
specified number of units to each other.
The value must be greater than 0.
Behaviors
---------
The :method:`~db.collection.createIndex()` method has the behaviors
described here.
.. warning::
Foreground index builds block all other operations on the database.
.. include:: /includes/extracts/createIndex-behavior.rst
- .. include:: /includes/fact-index-key-length-operation-behaviors.rst
:start-after: index-field-limit-ensureIndex
:end-before: .. index-field-limit-reIndex
.. |limit| replace:: :limit:`Maximum Index Key Length <Index Key>`
Examples
--------
Create an Ascending Index on a Single Field
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following example creates an ascending index on the field
``orderDate``.
.. code-block:: javascript
db.collection.createIndex( { orderDate: 1 } )
If the ``keys`` document specifies more than one field, then
:method:`~db.collection.createIndex()` creates a :term:`compound
index`.
Create an Index on a Multiple Fields
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following example creates a compound index on the
``orderDate`` field (in ascending order) and the ``zipcode``
field (in descending order.)
.. code-block:: javascript
db.collection.createIndex( { orderDate: 1, zipcode: -1 } )
A compound index cannot include a :ref:`hashed index <index-type-hashed>`
component.
.. note::
The order of an index is important for supporting
:method:`~cursor.sort()` operations using the index.
Additional Information
----------------------
- The :doc:`/indexes` section of this manual for full
documentation of indexes and indexing in MongoDB.
- :method:`db.collection.getIndexes()` to view the specifications of
existing indexes for a collection.
- :doc:`/core/index-text` for details on creating ``text``
indexes.
- :ref:`index-feature-geospatial` and
:ref:`index-geohaystack-index` for geospatial queries.
- :ref:`index-feature-ttl` for expiration of data.