forked from mongodb/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexes.txt
295 lines (216 loc) · 9.67 KB
/
indexes.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
.. _indexes:
=======
Indexes
=======
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: twocols
Indexes support the efficient execution of queries in MongoDB. Without
indexes, MongoDB must perform a *collection scan*, i.e. scan every
document in a collection, to select those documents that match the
query statement. If an appropriate index exists for a query,
MongoDB can use the index to limit the number of documents it must
inspect.
Indexes are special data structures [#b-tree]_ that store a small
portion of the collection's data set in an easy to traverse form. The
index stores the value of a specific field or set of fields, ordered by
the value of the field. The ordering of the index entries supports
efficient equality matches and range-based query operations. In
addition, MongoDB can return sorted results by using the ordering in
the index.
The following diagram illustrates a query that selects and orders the
matching documents using an index:
.. include:: /images/index-for-sort.rst
Fundamentally, indexes in MongoDB are similar to indexes in other
database systems. MongoDB defines indexes at the :term:`collection`
level and supports indexes on any field or sub-field of the documents
in a MongoDB collection.
.. index:: _id index
.. index:: _id
.. index:: index; _id
.. index:: index types; primary key
.. _index-type-id:
Default ``_id`` Index
---------------------
MongoDB creates a :ref:`unique index <index-type-unique>` on the
:ref:`_id <document-id-field>` field during the creation of a
collection. The ``_id`` index prevents clients from inserting two
documents with the same value for the ``_id`` field. You cannot drop
this index on the ``_id`` field.
.. note::
In :term:`sharded clusters <sharded cluster>`, if you do *not* use
the ``_id`` field as the :term:`shard key`, then your application
**must** ensure the uniqueness of the values in the ``_id`` field
to prevent errors. This is most-often done by using a standard
auto-generated :term:`ObjectId`.
Create an Index
---------------
To create an index, use :method:`db.collection.createIndex()` or a
similar :api:`method from your driver <>`.
.. code-block:: javascript
db.collection.createIndex( <key and index type specification>, <options> )
The :method:`db.collection.createIndex()` method only creates an index
if an index of the same specification does not already exist.
.. [#b-tree] MongoDB indexes use a B-tree data structure.
.. _index-types:
Index Types
-----------
MongoDB provides a number of different index types to support specific
types of data and queries.
.. _index-intro-single-field:
Single Field
~~~~~~~~~~~~
In addition to the MongoDB-defined ``_id`` index, MongoDB supports the
creation of user-defined ascending/descending indexes on a :doc:`single
field of a document </core/index-single>`.
.. include:: /images/index-ascending.rst
For a single-field index and sort operations, the sort order (i.e.
ascending or descending) of the index key does not matter because
MongoDB can traverse the index in either direction.
See :doc:`/core/index-single` and :ref:`sort-results-single-field` for
more information on single-field indexes.
Compound Index
~~~~~~~~~~~~~~
MongoDB also supports user-defined indexes on multiple fields, i.e.
:doc:`compound indexes </core/index-compound>`.
The order of fields listed in a compound index has significance. For
instance, if a compound index consists of ``{ userid: 1, score: -1 }``,
the index sorts first by ``userid`` and then, within each ``userid``
value, sorts by ``score``.
.. include:: /images/index-compound-key.rst
For compound indexes and sort operations, the sort order (i.e.
ascending or descending) of the index keys can determine whether the
index can support a sort operation. See
:ref:`index-ascending-and-descending` for more information on the
impact of index order on results in compound indexes.
See :doc:`/core/index-compound` and :ref:`sort-on-multiple-fields` for
more information on compound indexes.
Multikey Index
~~~~~~~~~~~~~~
MongoDB uses :doc:`multikey indexes </core/index-multikey>` to index
the content stored in arrays. If you index a field that holds an array
value, MongoDB creates separate index entries for *every* element of
the array. These :doc:`multikey indexes </core/index-multikey>` allow
queries to select documents that contain arrays by matching on element
or elements of the arrays. MongoDB automatically determines whether to
create a multikey index if the indexed field contains an array value;
you do not need to explicitly specify the multikey type.
.. include:: /images/index-multikey.rst
See :doc:`/core/index-multikey` and :doc:`/core/multikey-index-bounds`
for more information on multikey indexes.
Geospatial Index
~~~~~~~~~~~~~~~~
To support efficient queries of geospatial coordinate data, MongoDB
provides two special indexes: :doc:`2d indexes </core/2d>` that uses
planar geometry when returning results and :doc:`2dsphere indexes
</core/2dsphere>` that use spherical geometry to return results.
See :doc:`/core/geospatial-indexes` for a high level introduction to
geospatial indexes.
Text Indexes
~~~~~~~~~~~~
MongoDB provides a ``text`` index type that supports searching
for string content in a collection. These text indexes do not store
language-specific *stop* words (e.g. "the", "a", "or") and *stem* the
words in a collection to only store root words.
See :doc:`/core/index-text` for more information on text indexes and
search.
Hashed Indexes
~~~~~~~~~~~~~~
To support :ref:`hash based sharding <sharding-hashed-sharding>`,
MongoDB provides a :doc:`hashed index </core/index-hashed>` type,
which indexes the hash of the value of a field. These indexes have a
more random distribution of values along their range, but *only*
support equality matches and cannot support range-based queries.
Index Properties
----------------
Unique Indexes
~~~~~~~~~~~~~~
The :doc:`unique </core/index-unique>` property for an index causes
MongoDB to reject duplicate values for the indexed field. Other than
the unique constraint, unique indexes are functionally interchangeable
with other MongoDB indexes.
Partial Indexes
~~~~~~~~~~~~~~~
.. versionadded:: 3.2
:doc:`Partial indexes </core/index-partial>` only index the documents in
a collection that meet a specified filter expression. By indexing a
subset of the documents in a collection, partial indexes have lower
storage requirements and reduced performance costs for index creation
and maintenance.
Partial indexes offer a superset of the functionality of sparse indexes
and should be preferred over sparse indexes.
Sparse Indexes
~~~~~~~~~~~~~~
The :doc:`sparse </core/index-sparse>` property of an index ensures
that the index only contain entries for documents that have the indexed
field. The index skips documents that *do not* have the indexed field.
You can combine the sparse index option with the unique index option
to reject documents that have duplicate values for a field but ignore
documents that do not have the indexed key.
TTL Indexes
~~~~~~~~~~~
:doc:`TTL indexes </core/index-ttl>` are special indexes that MongoDB
can use to automatically remove documents from a collection after a
certain amount of time. This is ideal for certain types of information
like machine generated event data, logs, and session information that
only need to persist in a database for a finite amount of time.
See: :doc:`/tutorial/expire-data` for implementation instructions.
Index Use
---------
Indexes can improve the efficiency of read operations. The
:doc:`/tutorial/analyze-query-plan` tutorial provides an example of the
execution statistics of a query with and without an index.
For information on how MongoDB chooses an index to use, see :ref:`query
optimizer <read-operations-query-optimization>`.
Covered Queries
---------------
When the query criteria and the :term:`projection` of a query include
*only* the indexed fields, MongoDB will return results directly from
the index *without* scanning any documents or bringing documents into
memory. These covered queries can be *very* efficient.
.. include:: /images/index-for-covered-query.rst
For more information on covered queries, see
:ref:`read-operations-covered-query`.
Index Intersection
------------------
.. versionadded:: 2.6
MongoDB can use the :doc:`intersection of indexes
</core/index-intersection>` to fulfill queries. For queries that
specify compound query conditions, if one index can fulfill a part of a
query condition, and another index can fulfill another part of the
query condition, then MongoDB can use the intersection of the two
indexes to fulfill the query. Whether the use of a compound index or
the use of an index intersection is more efficient depends on the
particular query and the system.
For details on index intersection, see :doc:`/core/index-intersection`.
Restrictions
------------
Certain restrictions apply to indexes, such as the length of the index
keys or the number of indexes per collection. See :ref:`Index
Limitations <index-limitations>` for details.
Additional Considerations
-------------------------
Although indexes can improve query performances, indexes also present
some operational considerations. See :ref:`Operational Considerations
for Indexes <data-model-indexes>` for more information.
.. include:: /includes/index-tutorials-considerations.rst
.. class:: hidden
.. toctree::
:titlesonly:
/core/index-single
/core/index-compound
/core/index-multikey
/core/index-text
/core/2dsphere
/core/2d
/core/index-hashed
/core/index-properties
/core/index-creation
/core/index-intersection
/tutorial/manage-indexes
/tutorial/measure-index-use
/applications/indexes
/reference/indexes