From version 4.2, the copyDatabase is deprecated. From now on we should use: mongodump and mongorestore.
Let's say we have a database named: old_name and we want to rename it to new_name.
First we have to dump the database:
mongodump --archive="old_name_dump.db" --db=old_name
If you have to authenticate as a user then use:
mongodump -u username --authenticationDatabase admin \
--archive="old_name_dump.db" --db=old_name
Now we have our db dumped as a file named: old_name_dump.db.
To restore with a new name:
mongorestore --archive="old_name_dump.db" --nsFrom="old_name.*" --nsTo="new_name.*"
Again, if you need to be authenticated add this parameters to the command:
-u username --authenticationDatabase admin