Dump and Restore between version 3.6 and 5.0

Hi @shelton_zhang

Yes You would need to use the [mongodump](https://www.mongodb.com/docs/database-tools/mongodump/#mongodump) and [mongorestore](https://www.mongodb.com/docs/database-tools/mongorestore/) before you change the image from 4.4 to 7.0

You need to perform the steps below steps to do the upgrade.
If you have the mongo:4.4 running on a container named mongo4.4and has collections as

> db.example.find()
{ "_id" : ObjectId("66223ebb7d3bbdb1dfd8a3ae"), "name" : "a" }
{ "_id" : ObjectId("66223ebf7d3bbdb1dfd8a3af"), "name" : "b" }
>

Step1: Create the backup from the container mongo4.4

docker exec mongo4.4 mongodump --out /backups

Step 2: This backup is stored inside the container, so we need to copy the backup from the container to local

docker cp mongo4.4:/backups /<path to local>

Step 3: Copy this backup to the new container (say mongolatest running image mongo:latest)

docker cp /Users/aasawari.sahasrabuddhe/Downloads/backup/ mongolatest:/backups/

Step 4: Perform the restore on the latest container

aasawari.sahasrabuddhe@M-C02DV42LML85 Downloads % docker exec mongolatest mongorestore /backups

2024-04-19T10:20:01.209+0000	preparing collections to restore from
2024-04-19T10:20:01.209+0000	reading metadata for test.example from /backups/test/example.metadata.json
2024-04-19T10:20:01.219+0000	restoring test.example from /backups/test/example.bson
2024-04-19T10:20:01.234+0000	finished restoring test.example (2 documents, 0 failures)
2024-04-19T10:20:01.234+0000	no indexes to restore for collection test.example
2024-04-19T10:20:01.234+0000	2 document(s) restored successfully. 0 document(s) failed to restore.

Step 5: Validate if the data has been restored

aasawari.sahasrabuddhe@M-C02DV42LML85 Downloads % docker exec -it mongolatest mongosh
Current Mongosh Log ID:	66224568f7e58a3b14c934dc
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.4
Using MongoDB:		7.0.8
Using Mongosh:		2.2.4

For mongosh info see: https://docs.mongodb.com/mongodb-shell/
test> show dbs
admin    40.00 KiB
config  108.00 KiB
local    40.00 KiB
test      8.00 KiB
test> use test
already on db test
test> show collections
example
test> db.example.find()
[
  { _id: ObjectId('66223ebb7d3bbdb1dfd8a3ae'), name: 'a' },
  { _id: ObjectId('66223ebf7d3bbdb1dfd8a3af'), name: 'b' }
]

Please follow the above steps to perform the upgrade. Also the recommendation would be to perform rolling upgrade in order to avoid any failures.

Please don’t hesitate to reach out for any further questions.

Best Regards
Aasawari

2 Likes