我有一个应用程序,我想连接到相同的mongodb服务器,但不同的数据库,我使用本机驱动程序,它会引发错误。有没有更好的方法来使用本机驱动程序。
答案 0 :(得分:0)
我想你可以通过制作两个mongodbClient实例来实现它。然后连接不同的数据库。
var MongoClient1 = require('mongodb').MongoClient;
var MongoClient2 = require('mongodb').MongoClient;
MongoClient1.connect("mongodb://localhost:27017/exampleDb1", function(err, db) {
if(err) { return console.dir(err); }
db.collection('test', function(err, collection) {});
});
MongoClient2.connect("mongodb://localhost:27017/exampleDb2", function(err, db) {
if(err) { return console.dir(err); }
db.collection('test', function(err, collection) {});
});