Copying MongoDB Database into Local Machine

There is copy database command which I guess should be good fit for your need.

db.copyDatabase("DATABASENAME", "DATABASENAME", "localhost:27018");

Alternatively, you can just stop MongoDb, copy the database files to another server and run an instance of MongoDb there.


EDIT 2020-04-25

Quote from MongoDB documentation

MongoDB 4.0 deprecates the copydb and the clone commands and their mongo shell helpers db.copyDatabase() and db.cloneDatabase().

As alternatives, users can use mongodump and mongorestore (with the mongorestore options --nsFrom and --nsTo) or write a script using the drivers.

Reference here


I do this by creating a dump of the remote db to my local machine, which I then restore:

  1. Make sure you have a mongo instance up and running (eg. run mongod.exe from your bin folder in a terminal window. On my windows computer that's C:\mongodb\bin)

  2. Make a dump from remote db: Open a new terminal window, move to the bin folder again, run:

    mongodump -h example.host.com --port 21018 -d dbname --username username --password yourpass

    (Change the parameters to suit your own situation.)

  3. Restore the dumped database: Once the dump has been made, run the following command so that you have a local db:

    mongorestore -d theNameYouWantForYourLocalDB dump\nameOfRemoteDB

    (replace nameOfRemoteDB with the name of the remote db, the same as in previous command, and replace theNameYouWantForYourLocalDB with the name that you want your new local db to have)