What is the proper way of restoring the backup of a single collection downloaded locally? I can see the files structure for specific collection are the following:
*.json.gz files
metadata.json
The metada.json file looks like an output of the mongodump command while the .json.gz files look like they are from mongoexport.
What should be the recommended way to restore such structure?
Since there wasn’t a reply to your message, I tried to run it by our AI bot. Here is the response I got
Based on the file structure you’ve described, it appears you have a backup that combines elements from different MongoDB backup methods. The .json.gz files are likely from mongoexport, while the metadata.json file is typically associated with mongodump output. To restore this backup, we’ll need to use a combination of mongoimport and mongorestore. 1
Here’s a step-by-step procedure to restore your backup:
1. Prepare the Data
First, you need to extract the .json.gz files:
bash
gunzip -r <downloadFolder>
2. Import the Collection Data
Use mongoimport to import the JSON files into your MongoDB instance:
After completing these steps, connect to your MongoDB instance and verify that the collection has been restored correctly, including all documents and indexes.
Important Considerations:
The --mode=upsert option in the mongoimport command allows handling of duplicate documents from the archive. 1
Make sure you have sufficient permissions to perform these operations on the target database.
If you’re restoring to a replica set or sharded cluster, ensure you’re connecting to the primary node or a mongos instance.
Be cautious when restoring to an existing database/collection, as this process may overwrite existing data. Consider restoring to a temporary database first for verification before replacing your production data.
If you encounter any issues with index recreation, you may need to manually create the indexes using the information from the metadata.json file.
By following these steps, you should be able to restore your single collection backup successfully. Remember to adjust the database name, collection name, and file paths according to your specific backup files and target MongoDB instance. 12
Although, I’m afraid the documentation bot is wrong here. In the 3rd part it suggests to run mongorestore with the path to metadata.json file. But AFAIK mongorestore doesn’t support the JSON input but only BSON format, is that correct?