Won't able to transfer data from one database to another using mongorestore tool

Hi Community,

I’ve developed a shell script to transfer data from one Atlas Cluster to an EC2 cluster using the following mongodump and mongorestore commands:

mongodump --uri="$MONGO_SOURCE_URI" --collection="$COLLECTION_NAME" --out=$BACKUP_DIR/ --gzip --numParallelCollections=1 >> "$DUMP_LOG_FILE" 2>&1

Restore option: 1

mongorestore --uri="$MONGO_DEST_URI" --gzip --nsInclude="target_db.$COLLECTION_NAME" $BACKUP_DIR/source_db/$COLLECTION_NAME.bson.gz >> "$RESTORE_LOG_FILE" 2>&1

Restore option: 2

mongorestore --uri="$MONGO_DEST_URI" --gzip --nsFrom="source_db.$COLLECTION_NAME" --nsTo="target_db.$COLLECTION_NAME" $BACKUP_DIR/source_db/$COLLECTION_NAME.bson.gz >> "$RESTORE_LOG_FILE" 2>&1

While using the first option, the data is restored into a database with the same name as source_db. To prevent this, I tried the second option, but the data still gets transferred into a database with a similar name to source_db.

I’m unable to figure out why mongorestore behaves this way. Any insights or help would be greatly appreciated.

Thanks!

Reference of option 2: https://stackoverflow.com/questions/36321899/mongorestore-to-a-different-database