Search code examples
mongodblocalhostlocaldatabase

Copying MongoDB Database into Local Machine


I have a MongoDB database that resides on a remote server machine whose IP address is 192.168.1.20 on a local network. For development and testing purposes, and since I am not allowed to modify or delete the database on the server for security purposes, I want to copy the database on my local machine for my personal use.

Can anyone please tell me, how do I achieve this?


Solution

  • 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