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

mongorestore: NamespaceExists Error even though database was empty and --drop was used

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None

      Problem Statement/Rationale

      We are Mongo Atlas customers and we are using mongodump to create an additional backup on our local machines every night from our production database. We are also using mongorestore to copy that dump data to another staging database, so we have real data there. That database is also in Mongo Atlas. The problem is that recently in most cases that fails, we get the error message:
      Failed: databaseName.collectionName: error creating collection databaseName.collectionName: error running create command: (NamespaceExists) Collection databaseName.collectionName
      It doesn't fail on the same collection every time, it's always a different collection. Sometimes it works without any problems.

      Steps to Reproduce

      My mongodump command is this:

      $pathToMongoTools/mongodump \ 
          --uri "mongodb+srv://${prodUser}:${prodPassword}@${prodUri}" \ 
          --ssl \ 
          -o ${dumpname}_${dumpdate}
      

      (prodUri also includes the database name.)

      Then, in addition I run these commands on the staging database (from a script):

      use databaseName;
      db.dropDatabase();
      

      After that I try to restore it to the staging database with this command:

      $pathToMongoTools/mongorestore \ 
          -j=1 \ 
          --maintainInsertionOrder \ 
          --ssl \ 
          --drop \ 
          --stopOnError \ 
          -vvvvv \ 
          --uri "mongodb+srv://${stagingAdminUser}:${stagingAdminPassword}@${stagingUri}" \ 
          ${dumpname}_${dumpdate}
      

      The script with the db.dropDatabase() and the j=1 and maintainInsertionOrder were added later by me to try to stop these errors. At first they seemed to help but now the errors occur again even with those options.

      Expected Results

      I expect that the restore works reliably every time.

      Actual Results

      It fails pretty often, just today I ran the mongorestore with -vvvvv and it showed this error:

      [...]
      2023-11-03T03:13:47.519+0100	collection databaseName.collectionName doesn't exist, skipping drop command
      2023-11-03T03:13:47.519+0100	creating collection databaseName.collectionName with no metadata
      2023-11-03T03:13:47.519+0100	using collection options: primitive.D\{primitive.E{Key:"idIndex", Value:idx.IndexDocument{Options:primitive.M{"name":"_id_", "ns":"databaseName.collectionName"}, Key:primitive.D\{primitive.E{Key:"_id", Value:1}}, PartialFilterExpression:primitive.D(nil)}}}
      2023-11-03T03:13:47.577+0100	finished restoring databaseName.collectionName (0 documents, 0 failures)
      2023-11-03T03:13:47.577+0100	Failed: databaseName.collectionName: error creating collection databaseName.collectionName: error running create command: (NamespaceExists) Collection databaseName.collectionName already exists.
      2023-11-03T03:13:47.577+0100	205123 document(s) restored successfully. 0 document(s) failed to restore.
      

      And it stops the restore there.

      Additional Notes

      Any additional information that may be useful to include.

            Assignee:
            dave.rolsky@mongodb.com Dave Rolsky
            Reporter:
            aer@toolmaker.de Andreas Ermler
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: