Cukup gunakan URL Koneksi Mongo, seperti yang dijelaskan di mogo dokumen:
mongodb://[username:[email protected]]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
Jadi kode Anda akan terlihat seperti ini (sesuai dengan manual modul npm mongodb ):
const MongoClient = require('mongodb');
// Connection URL
const url = 'mongodb://[username:[email protected]]host1[:port1][,host2[:port2],...[,hostN[:portN]]]';
// Database Name
const dbName = '[dbName]';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = client.db(dbName);
client.close();
});
Semoga berhasil!