how to copy mongodb database from another server code example
Example: mongo copy database
Before MongoDB version 4.2
source_db.copyDatabase('destination_db')
After MongoDB version 4.2
mongodump --archive="mongodump-source-db" --db=source_db
mongorestore --archive="mongodump-source-db" --nsFrom='source_db.*' --nsTo='destination_db.*'
Or,
mongodump --archive --db=source_db | mongorestore --archive --nsFrom='source_db.*' --nsTo='destination_db.*'