Copying Database in pymongo

I am trying to rename a database from within pymongo. I understand that the way to do this is to copy the database, then delete the original (right?), thus I am trying to run the code:

client = MongoClient(connectionString, server_api=ServerApi('1'))
client.admin.command('copydb',
                         fromdb=dbName,
                         todb=newDBName)

which I copied from there:
https://pymongo.readthedocs.io/en/stable/examples/copydb.html

The user that successfully logged in has the Built-In role Atlas Admin. However, running this code gives me an error:
OperationFailure: (Unauthorized) not authorized on admin to execute command

How is this possible, doesn’t the Atlas Admin have all the rights to do anything?

Hi @Dominik_Koller,

The copydb admin command was deprecated in MongoDB 4.0 and removed in MongoDB 4.2. The removal notice includes an alternative approach using mongodump and mongorestore (which has --nsFrom and --nsTo options to rename a namespace).

If you’re wondering why copydb was removed (quoting an answer from myself on Reddit):

The copydb command (and associated shell helpers) had a number of unfortunate caveats including limited auth support, not taking a point-in-time snapshot, no support for sharding, and foreground index builds (which will take a lock on the target database).

The PyMongo example you found is unfortunately no longer relevant for modern versions of MongoDB server and should be removed (PYTHON-3138).

Atlas does not support commands that are taken care of by the management interface or not applicable to shared clusters. For more details, please see:

The server_api parameter in your code indicates you using the MongoDB 5.0+ Stable API, which is well after copydb was removed.

Regards,
Stennie

Great, thank you Stennie!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.