Only indexes that were created with {background: true}
originally (and hence have that option set in the system.indexes
collection) are built in the background once restored. Once you have the database dumped you will have your data in the name.bson
file and the metadata (indexes to build) in the name.metadata.json
file. Unlike the BSON file, the JSON metadata is just text and can be modified before you restore.
As a test, I created a collection foo.bar
, with 2 additional indexes besides the default on the a
field and on the b
field. Here is what the dumped bar.metadata.json
file looked like in the foo folder once I had dumped it out:
{ "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "foo.bar" }, { "v" : 1, "key" : { "a" : 1 }, "name" : "a_1", "ns" : "foo.bar" }, { "v" : 1, "key" : { "b" : 1 }, "name" : "b_1", "ns" : "foo.bar", "background" : true } ] }
You can "fix" this with your favorite editor and remove , "background" : true
with a find & replace or similar. Or you could use a sed
one liner, something like:
sed 's/, "background" : true//g' bar.metadata.json
Which, as expected, gives us:
{ "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "foo.bar" }, { "v" : 1, "key" : { "a" : 1 }, "name" : "a_1", "ns" : "foo.bar" }, { "v" : 1, "key" : { "b" : 1 }, "name" : "b_1", "ns" : "foo.bar" } ] }
I tested a restore with the above bar.metadata.json file and my indexes were no longer created in the background, and were not marked as such in system.indexes
any longer.