There is a database that consists of thousands of collections, and we want to export and import those collections. Also, this will be between different MongoDB versions, so we cannot use mongodump and mongorestore. Is there an easy way to do this?


  • How about first installing the same Mongo version to the second location, moving collections there, and then doing a version upgrade?
    – JJussi
    Aug 3, 2023 at 13:19

1 Answer 1


You can try something like this:

cmd='db.getSiblingDB("config").collections.find().forEach(x => { print(`--db=${x._id.split(".").shift()} --collection=${x._id.split(".").slice(1).join(".")}`) })'
for ns in $( mongosh "mongodb://localhost:27017/..." --eval "${cmd}") ; do
    mongoexport --uri="mongodb://localhost:27017/..." ${ns} | mongoimport --uri="mongodb://new_host:27017/..." --numInsertionWorkers=10 ${ns}

However, first I would try with

mongodump --uri="mongodb://localhost:27017/..." ${ns} | mongorestore --uri="mongodb://new_host:27017/..." 

because it also creates indexes and privileges. With mongoexport/mongoimport you need to create them all manually. I would expect mongodump/mongorestore will also work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.