使用Mongoose@4.2.6,我想连接到分片的MongoDB实例。
const url = "mongodb://user:pass@localhost:27022,localhost:27023/db";
// This never resolves
Mongoose.connect(url, (err, db) => {
...
});
当我使用本机MongoDB@2.0.48库(与Mongoose使用的版本相同)进行连接时,它可以正常工作。
const url = "mongodb://user:pass@localhost:27022,localhost:27023/db";
// This resolves a connection
MongoClient.connect(url, (err, db) => {
...
});
当我在Mongoose中建立连接时,连接回调永远不会解决。
Mongoose中是否有必须进行配置以允许我连接到分片的Mongo实例?
由于
答案 0 :(得分:2)
需要将对象作为第二个参数传递,值为mongos = true
const url = "mongodb://user:pass@localhost:27022,localhost:27023/db";
// This now resolves
Mongoose.connect(url, { mongos: true }, (err, db) => {
...
});
从文档中可以非常清楚,但它存在 - http://mongoosejs.com/docs/connections.html