export mongodb connection code example
Example 1: module.exports mongodb connection
const MongoClient = require( 'mongodb' ).MongoClient;
const url = "mongodb://localhost:27017";
var _db;
module.exports = {
connectToServer: function( callback ) {
MongoClient.connect( url, { useNewUrlParser: true }, function( err, client ) {
_db = client.db('test_db');
return callback( err );
} );
},
getDb: function() {
return _db;
}
};
Example 2: how to export mongodb database
Mongo Export/dump
mongodump --out </path> --db <dbName> --host <hostname:portNo> --authenticationDatabase admin --ssl --username <Remote Db User> --password<Remote Db Password>
Example:
mongodump --out ~/Desktop/mongoData/ --db newdata --host cluster0-shard-01-02.xyz.mongodb.net:27017 --authenticationDatabase admin --ssl --username abc --password xyz
Mongo Import/restore
mongorestore </path> --db <dbName> --host <hostname:portNo> --authenticationDatabase admin --ssl --username <Remote Db User> --password<Remote Db Password>
Example:
mongorestore ~/Desktop/mongobkup/ --host cluster0-shard-00-02.xyz.mongodb.net:27017 --authenticationDatabase admin --ssl --username abc --password xyz