|
1 |
| -================================ |
2 |
| -Roll Back to a Version 1.8 Index |
3 |
| -================================ |
| 1 | +======================= |
| 2 | +Build Old Style Indexes |
| 3 | +======================= |
4 | 4 |
|
5 | 5 | .. default-domain:: mongodb
|
6 | 6 |
|
| 7 | +.. important:: |
7 | 8 |
|
8 |
| -MongoDB version 2.0 and later supports the old index format. But old versions |
9 |
| -will not support the new format. If you need to roll back to an older |
10 |
| -version, the server will run, but queries and other operations involving |
11 |
| -the newer indexes will log and return an error. Thus, you will need to |
12 |
| -re-create any new index you would like to use on an old server. |
13 |
| - |
14 |
| -Versions prior to 1.8.2, inclusive, are not aware of the index version |
15 |
| -field. If you rollback a ``{v:1}`` index to 1.8.2 and re-index it, its |
16 |
| -version will still be marked ``{v: 1}``, although it actual is now version ``{v:0}``. |
17 |
| -If you upgrade again to 2.0, this index will not work, even though it is |
18 |
| -marked as ``{v: 1}`` in ``system.indexes``. If you must roll back to a |
19 |
| -version prior to 1.8.2, you must delete the index then create it again |
20 |
| -(instead of simply re-indexing). |
21 |
| - |
22 |
| -Building a {v:0} Index |
23 |
| ----------------------- |
24 |
| - |
25 |
| -You can still create a ``{v:0}`` index with MongoDB version 2.0 or later. To do so, add the |
26 |
| -option ``{v:0}`` in the index creation command. For example in the :program:`mongo` |
27 |
| -shell: |
28 |
| - |
29 |
| -.. code-block:: javascript |
30 |
| - |
31 |
| - // defaults to a v:1 index |
32 |
| - db.foo.ensureIndex({name:1}) |
33 |
| - db.system.indexes.find() |
34 |
| - { "v" : 1, "key" : { "_id" : 1 }, "ns" : "mydb.foo", "name" : "_id_" } |
35 |
| - { "v" : 1, "key" : { "name" : 1 }, "ns" : "mydb.foo", "name" : "name_1" } |
36 |
| - db.foo.dropIndex({name:1}) |
37 |
| - { "nIndexesWas" : 2, "ok" : 1 } |
38 |
| - |
39 |
| - // create a v:0 index |
40 |
| - db.foo.ensureIndex({name:1},{v:0}) |
41 |
| - db.system.indexes.find() |
42 |
| - { "v" : 1, "key" : { "_id" : 1 }, "ns" : "mydb.foo", "name" : "_id_" } |
43 |
| - |
44 |
| - { "v" : 0, "key" : { "name" : 1 }, "ns" : "mydb.foo", "name" : "name_1" } |
45 |
| - |
46 |
| -.. seealso:: :ref:`2.0-new-index-format` and :ref:`2.0-convert-to-new-index-format` |
| 9 | + Use this procedure *only* if you must have indexes that are compatible |
| 10 | + with a version of MongoDB earlier than 2.0. |
| 11 | + |
| 12 | +MongoDB version 2.0 introduced the ``{v:1}`` index format. MongoDB |
| 13 | +versions 2.0 and later support both the ``{v:1}`` format and the |
| 14 | +earlier ``{v:0}`` format. |
| 15 | + |
| 16 | +MongoDB versions prior to 1.8.2, however, support only the ``{v:0}`` |
| 17 | +format. If you need to roll back MongoDB to a version prior to 2.0, you |
| 18 | +must *drop* and *re-create* your indexes. |
| 19 | + |
| 20 | +You do so using the :method:`dropIndexes() |
| 21 | +<db.collection.dropIndexes()>` and :method:`ensureIndex() |
| 22 | +<db.collection.ensureIndex()>` methods. You *cannot* simply reindex the |
| 23 | +collection. If you simply reindex, the ``v`` fields in your indexes |
| 24 | +would still hold values of ``1``, even though the indexes would now use |
| 25 | +the ``{v:0}`` format. If you were to upgrade again to version 2.0 or |
| 26 | +later, your indexes would not work. |
| 27 | + |
| 28 | +.. example:: |
| 29 | + |
| 30 | + Suppose you rolled back from MongoDB 2.0 to MongoDB 1.8, and suppose |
| 31 | + you had the following index on the ``items`` collection: |
| 32 | + |
| 33 | + .. code-block:: javascript |
| 34 | + |
| 35 | + { "v" : 1, "key" : { "name" : 1 }, "ns" : "mydb.items", "name" : "name_1" } |
| 36 | + |
| 37 | + The ``v`` field tells you the index is a ``{v:1}`` index, which |
| 38 | + is incompatible with version 1.8. |
| 39 | + |
| 40 | + To drop the index, issue the following command: |
| 41 | + |
| 42 | + .. code-block:: javascript |
| 43 | + |
| 44 | + db.items.dropIndex( { name : 1 } ) |
| 45 | + |
| 46 | + To recreate the index as a ``{v:0}`` index, issue the following |
| 47 | + command: |
| 48 | + |
| 49 | + .. code-block:: javascript |
| 50 | + |
| 51 | + db.foo.ensureIndex( { name : 1 } , { v : 0 } ) |
| 52 | + |
| 53 | +.. seealso:: :ref:`2.0-new-index-format`. |
0 commit comments