I’m using mongoDB 6.0.0 community and automated backup and restore database A to another server in batch script. However , by using mongorestore with -drop cant help to delete the collection not exist in backup.
would like to seek advise on how to drop existing database A and restore latest backup file in another server as would like to have same set of collections with original server.
Hi @lin_choong, welcome to the forums.
I think there are two ways to approach this.
- Start a brand new instance. Stop mongod, remove the file in the dbpath and start mongo again*
- Run a script to iterate over the existing databases and drop them before running the restore.
*Could be more involved if you need to initialise a replica set or have a sharded cluster, in which case option 2 might be simpler.
Hi Thanks for the reply.
I would like to proceed with approach #2, however i tried to use batch script earlier seem its not working . any idea of the what scripting language i can use to connect mongodb and drop the certain database?
Mongosh and javascript:
mongosh --quiet --eval 'db.adminCommand({listDatabases:1,nameOnly:true}).databases.filter(x => ["admin","config","local"].indexOf(x.name)<0).map(x => db.getSiblingDB(x.name).dropDatabase())'
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.