Uploaded image for project: 'MongoDB Database Tools'
  1. MongoDB Database Tools
  2. TOOLS-2321

Mongorestore fails to restore a collection if the _id: 1 index has unique: true flag set

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: mongorestore
    • Labels:
      None

      Starting from v3.4 createIndexes no longer accepts the unique: true option if an _id:1 index is attempted to be created explicitly (SERVER-26724).

      However it is absolutely possible for users to have many collections with _id indexes having the unique: true flag set that were carried on with an upgrade from an older version. These indexes work just fine (can be replicated etc) but there is no easy way to have them recreated without the unique: true flag.

      The pain for the users is that if they try to restore a dump of these collections then mongorestore will fail:

      dmitry@lubuntu:/tmp/dd/dump/test$ mongorestore -u admin -p 123 --authenticationDatabase admin -d test -c c2 c.bson 
      2019-06-28T13:07:19.492+1000	checking for collection data in c.bson
      2019-06-28T13:07:19.494+1000	reading metadata for test.c2 from c.metadata.json
      2019-06-28T13:07:19.496+1000	Failed: test.c2: error creating collection test.c2: error running create command: The field 'unique' is not valid for an _id index specification. Specification: { v: 2, unique: true, name: "_id_", ns: "test.c2", key: { _id: 1 } }
      

      The only workaround is to have the *.metadata.json files edited to with the unique: true option removed (in case of a large number of the affected collections that would be cumbersome).

      I think the best solution would be if the mongorestore would automatically omit the unique: true option if it's set for an _id: 1 index.

            Assignee:
            Unassigned Unassigned
            Reporter:
            dmitry.ryabtsev@mongodb.com Dmitry Ryabtsev
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: