Migrate data
Whitelist your IP in Moralis
In order to access your MongoDB you need to whitelist your IP address:
Connect directly to the hosted Moralis MongoDB
You can connect to the MongoDB that is hosted on Moralis via different methods:
- Using scripts
- Using the MongoDB Compass tool
- Using
mongodump
andmongorestore
Using scripts
- NodeJs
- Python
const { MongoClient } = require('mongodb');
const MONGO_HOST = 'MONGO_HOST_IP_FROM_ADMIN_INTERFACE';
const MONGO_PORT = 'MONGO_HOST_PORT_FROM_ADMIN_INTERFACE';
// Create a new MongoClient
const client = new MongoClient(`mongodb://${MONGO_HOST}:${MONGO_PORT}`);
async function run() {
try {
// Connect the client to the server
await client.connect();
// Establish and verify connection
await client.db('admin').command({ ping: 1 });
console.log('Connected successfully to server');
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
import pprint
import pymongo
MONGO_HOST = "MONGO_HOST_IP_FROM_ADMIN_INTERFACE"
MONGO_PORT = MONGO_HOST_PORT_FROM_ADMMIN_INTERFACE
con = pymongo.MongoClient(MONGO_HOST, MONGO_PORT)
user_table = con['parse']['_User']
pprint.pprint(user_table.find_one())
After you have established direct access to Mongo DB, you You can do your own dumps for the database.
_User table is a particular example of table that starts with _, most of the tables will not start with _
Using Compass
You can use Compass to interact with MongoDB using a tool with an interface.
Now you are connected to the MongoDB that is hosted on Moralis. For reference on how to export data, see https://www.mongodb.com/docs/compass/current/import-export/.
Using mongodump and mongorestore
mongodump
and mongorestore
are command line tools you can use to perform a full database migration from your hosted server to your self-hosted server.
- Install MongoDB Database Tools. You can read the instructions specific to your OS here.
- Run the following command to dump your hosted server's database:
mongodump mongodb://ipaddress:port --db=parse
You should have a folder called dump
with a folder called parse
inside it.
- Change the terminal's directory to
dump
. - Run the following command to migrate the data to your new MongoDB database - this will copy the data from the
parse
folder to a database calledparse
(/parse
):
mongorestore mongodb+srv://username:password@***.mongodb.net/parse parse
Import data
To import data, you can use the same methods as in the previous step. You can use a script, or connect via Compass, or use mongorestore. The difference is that you will need to provide the DATABASE_URI
of your server that you have specified in your .env
.