# Transferring Tables From One MongoDB Database to Another
The process outlined in the server logs can be divided into a few major steps:
- Backup of collections from the source database.
- Modification of the backup files if necessary.
- Restoration of the backup into the target database.
# 1. Back Up the Collection From the Source Database
Use mongodump to back up the collection:
mongodump -u admin -p "<password>" --authenticationDatabase=admin --db <source_db> --collection <collection_name> --out /tmp/path/to/backup-folder
Replace <password>, <source_db>, <collection_name>, and /tmp/path/to/backup-folder with your MongoDB credentials, the name of the source database, the name of the collection you want to transfer, and the output path for the backup files, respectively.
# 2. (Optional) Modify the Backup Files
If necessary, you may want to modify the backup files. This may involve changing some database-specific strings in the backup file. Use sed to perform the modifications:
- Install
python3-pipandpymongoif you haven't already:
apt install python3-pip
pip install pymongo
- Create the replacement script
collection_replacer.py:
nano collection_replacer.py
import argparse
import bson
def replace_nested(obj, old_text, new_text):
if isinstance(obj, str):
return obj.replace(old_text, new_text)
elif isinstance(obj, dict):
return {k: replace_nested(v, old_text, new_text) for k, v in obj.items()}
elif isinstance(obj, list):
return [replace_nested(elem, old_text, new_text) for elem in obj]
else:
return obj
def main(bson_filepath, old_text, new_text):
# Read BSON file and decode to a Python dict
with open(bson_filepath, 'rb') as f:
bson_data = f.read()
data = bson.decode_all(bson_data)
# Perform string replacement
data = [replace_nested(d, old_text, new_text) for d in data]
# Encode dict back to BSON and write to file
with open(bson_filepath, 'wb') as f:
for d in data:
f.write(bson.BSON.encode(d))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Replace text in BSON file")
parser.add_argument("bson_filepath", help="Path to BSON file")
parser.add_argument("old_text", help="Text to replace")
parser.add_argument("new_text", help="Text to replace with")
args = parser.parse_args()
main(args.bson_filepath, args.old_
text, args.new_text)
python3 collection_replacer.py /tmp/new_ehs/settings.bson ehs/uploads ehs_migrated/uploads
Please note that binary files such as .bson files aren't meant to be manipulated using text processing tools like sed. This operation could potentially corrupt the file.
# 3. Restore the Backup Into the Target Database
Use mongorestore to restore the backup into the target database:
mongorestore -u admin -p "<password>" --authenticationDatabase=admin --db <target_db> --collection <collection_name> --drop /tmp/path/to/backup-folder/<collection_name>.bson
Replace <password>, <target_db>, <collection_name>, and /tmp/path/to/backup-folder with your MongoDB credentials, the name of the target database, the name of the collection, and the path to the backup files, respectively.
--drop is an optional flag that tells MongoDB to drop the collection from the target database if it exists before performing the restore operation.