@@ -35,26 +35,146 @@ View Existing Indexes
35
35
Remove Indexes
36
36
--------------
37
37
38
+ .. tip:: Hide an Index Before Dropping It
39
+
40
+ If you drop an index that is actively used in production, your
41
+ application may incur a performance degradation. Before you drop an
42
+ index, you can evaluate the potential impact of the drop by
43
+ :ref:`hiding the index <hide-existing-index>`.
44
+
45
+ Hidden indexes are not used to support queries. If you hide an index
46
+ and observe substantial negative performance impact, consider keeping
47
+ and unhiding the index so queries can resume using it.
48
+
38
49
.. include:: /includes/driver-remove-indexes-tabs.rst
39
50
40
51
Modify an Index
41
52
---------------
42
53
43
54
.. include:: /includes/driver-examples/driver-example-modify-index-tabs.rst
44
55
45
- .. tabs-drivers::
56
+ Minimize Performance Impact With a Temporary Index
57
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58
+
59
+ If you drop an index that is actively used in production, your
60
+ application may incur a performance degradation. To ensure queries can
61
+ still use an index during modification, you can create a temporary,
62
+ redundant index that contains the same fields as the modified index.
63
+
64
+ Example
65
+ ```````
66
+
67
+ This example creates a new index and modifies that index to make it
68
+ :ref:`unique <index-type-unique>`.
69
+
70
+ .. procedure::
71
+ :style: normal
72
+
73
+ .. step:: Create a ``siteAnalytics`` collection with an index on the ``url`` field
74
+
75
+ Run this command:
76
+
77
+ .. code-block:: javascript
78
+
79
+ db.siteAnalytics.createIndex( { "url": 1 } )
80
+
81
+ The command returns the name of the index:
82
+
83
+ .. code-block:: javascript
84
+ :copyable: false
85
+
86
+ url_1
87
+
88
+ .. step:: Create a temporary index that contains the ``url`` field
89
+
90
+ Run this command:
91
+
92
+ .. code-block:: javascript
93
+
94
+ db.siteAnalytics.createIndex( { "url": 1, "dummyField": 1 } )
95
+
96
+ The command returns the name of the index:
97
+
98
+ .. code-block:: javascript
99
+ :copyable: false
100
+
101
+ url_1_dummyField_1
102
+
103
+ This temporary index lets you safely drop the original ``{ "url":
104
+ 1 }`` index without impacting performance.
105
+
106
+ .. step:: Drop the original index
107
+
108
+ Run this command:
109
+
110
+ .. code-block:: javascript
111
+
112
+ db.siteAnalytics.dropIndex( { "url": 1 } )
113
+
114
+ The command returns:
115
+
116
+ .. code-block:: javascript
117
+ :copyable: false
118
+
119
+ { nIndexesWas: 3, ok: 1 }
120
+
121
+ .. step:: Recreate the ``{ "url": 1 }`` index with the ``unique`` property
122
+
123
+ Run this command:
124
+
125
+ .. code-block:: javascript
126
+
127
+ db.siteAnalytics.createIndex( { "url": 1 }, { "unique": true } )
128
+
129
+ The command returns the name of the index:
130
+
131
+ .. code-block:: javascript
132
+ :copyable: false
133
+
134
+ url_1
135
+
136
+ The ``url_1`` index is recreated and you can drop the temporary
137
+ index without impacting performance. Queries on the ``url`` field
138
+ can use the new unique index.
139
+
140
+ .. step:: Drop the temporary index
141
+
142
+ Run this command:
143
+
144
+ .. code-block:: javascript
145
+
146
+ db.siteAnalytics.dropIndex( { "url": 1, "dummyField": 1 } )
147
+
148
+ The command returns:
149
+
150
+ .. code-block:: javascript
151
+ :copyable: false
152
+
153
+ { nIndexesWas: 3, ok: 1 }
154
+
155
+ .. step:: Confirm that the index was updated
156
+
157
+ To view the indexes on the ``siteAnalytics`` collection, run this
158
+ command:
159
+
160
+ .. code-block:: javascript
161
+
162
+ db.siteAnalytics.getIndexes()
163
+
164
+ The command returns these indexes, indicating that the ``url_1``
165
+ index is now unique:
46
166
47
- tabs:
48
- - id: compass
49
- content: |
50
- .. seealso::
167
+ .. code-block:: javascript
168
+ :copyable: false
51
169
52
- - :ref:`MongoDB Compass Documentation <compass-index>`
53
- - :compass:`Compass Documentation for Indexes </indexes/>`
170
+ [
171
+ { v: 2, key: { _id: 1 }, name: '_id_' },
172
+ { v: 2, key: { url: 1 }, name: 'url_1', unique: true }
173
+ ]
54
174
55
175
.. _manage-indexes-find-inconsistent-indexes:
56
176
57
- Find Inconsistent Indexes across Shards
177
+ Find Inconsistent Indexes Across Shards
58
178
---------------------------------------
59
179
60
180
A sharded collection has an inconsistent index if the collection does
0 commit comments