9

I have a .bson file from a MongoDB dump. The dump also produces a .metadata.json file that seems to contain index definitions.

{
    "options": {},
    "indexes": [
        {
            "v": 1,
            "key": {
                "_id": 1
            },
            "name": "_id_",
            "ns": "test.oneMillionDocuments"
        }
    ]
}

I assume from this that restoring from the .bson file doesn't include the indexes.

  • How do I use the .metadata.json file? Do I need to restore it separately?

My restore finished with the following lines:

2016-02-06T19:18:56.397+0000    [#######################.]  test2.oneMillionRecordsRestore  9.7 GB/9.7 GB  (99.5%)
2016-02-06T19:18:58.475+0000    restoring indexes for collection test2.oneMillionRecordsRestore from metadata
2016-02-06T19:18:58.485+0000    finished restoring test2.oneMillionRecordsRestore (1000000 documents)
2016-02-06T19:18:58.488+0000    done

There's a line saying it's restoring the indexes, but specifically referencing the .metadata.json file.

BanksySan
  • 27,362
  • 33
  • 117
  • 216

1 Answers1

11

You don't have to do anything specific with the metadata.json file: just run mongorestore and it will be read together with the .bson files provided that it is in the same directory. You will see a line in the output from which you can infer whether it has been read or not:

2016-02-06T20:22:08.652+0100    reading metadata for mydb.message from dump/mydb/message.metadata.json

However, I think that there are cases where it is ignored (e.g. conflicting index definitions). The import won't fail though, but you will see a line in the log telling you that the metadata (or parts of it) were ignored.

Nicolas
  • 2,151
  • 1
  • 25
  • 29
  • Mine finished with a slightly different line... I guess I'll have to add some index other than `_id` to see it. – BanksySan Feb 06 '16 at 19:26
  • I've added how my restore finished to the question. – BanksySan Feb 06 '16 at 19:27
  • The line I mentioned is printed early in the log, before the data is inserted. The lines you copied confirm that `oneMillionRecordsRestore.metadata.json` was read: `mongorestore` can only find index definitions and therefore restore indexes for this collection in this file. – Nicolas Feb 06 '16 at 19:39
  • There it is! Thanks. – BanksySan Feb 06 '16 at 19:40
  • Prior to v3.2.1, a bug [truncated the metadata restoration log lines](https://jira.mongodb.org/browse/TOOLS-982) such that the `.metadata.json` filename was omitted. Fixed now (can confirm). – duozmo Feb 09 '16 at 23:21
  • 1
    What if I want to skip the .bson file and I want to restore only the .metadata.json? – bartimar Dec 06 '17 at 12:34