Brug bare Mongos forbindelses-URL, som beskrevet i mogo's dokumenter:
mongodb://[username:[email protected]]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
Så din kode ville se sådan ud (i overensstemmelse med mongodbs npm-modulmanual ):
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();
});
Held og lykke!