如何处理MongoClient中的超时错误?
我使用以下命令将nodejs应用程序连接到我的mongodb数据库:
const url = 'mongodb://localhost:27017/?replicaSet=rs'
let client = new MongoClient(url, {
useNewUrlParser: true,
connectTimeoutMS: 60000,
socketTimeoutMS: 60000,
})
try {
let dbclient = await client.connect()
console.log(dbclient)
const db = dbclient.db('test')
const collection = db.collection('accounts')
const changeStream = collection.watch(pipeline)
changeStream.on("change", function(change) {
console.log('changed', change)
})
} catch (err) {
console.log('mongo err:', err)
}
这很好用,但几分钟后它会经常失去连接错误:
未捕获的MongoNetworkError:连接6到localhost:27017超时
根据the documentation,它应该在出错时自动重新连接多达30次尝试,但它似乎没有进一步尝试重新连接。
我还需要在重新连接时运行一些额外的逻辑以正确处理本地状态。
我怎样才能抓住并处理这些错误?
编辑:虽然我还没有收到其他事件,但是在发生错误后我会收到“重新连接”事件。所以我似乎至少可以对这些错误作出反应,但实际上并没有抓住它们。
回答如下:只需添加连接keepAlive: true
,或特定时间keepAlive: 300000
请参阅此处的示例:https://ide.c9.io/ibrahimth/mongo
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://localhost:27017/?replicaSet=rs'
// Database Name
const dbName = 'test';
// create a client, passing in additional options
const client = new MongoClient(url, {
keepAlive: true,
connectTimeoutMS: 60000,
socketTimeoutMS: 60000,
});
// Use connect method to connect to the server
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected correctly to server");
const db = client.db(dbName);
createCollated(db, function() {
client.close();
});
});
function createCollated(db, callback) {
db.createCollection('acount',
{
'collation' :
{ 'acountnam': 'firstacount' }
},
function(err, results) {
console.log("Collection created.");
callback();
}
);
};
有关更多信息:http://mongodb.github.io/node-mongodb-native/3.0/tutorials/collations/
如何处理MongoClient中的超时错误?
我使用以下命令将nodejs应用程序连接到我的mongodb数据库:
const url = 'mongodb://localhost:27017/?replicaSet=rs'
let client = new MongoClient(url, {
useNewUrlParser: true,
connectTimeoutMS: 60000,
socketTimeoutMS: 60000,
})
try {
let dbclient = await client.connect()
console.log(dbclient)
const db = dbclient.db('test')
const collection = db.collection('accounts')
const changeStream = collection.watch(pipeline)
changeStream.on("change", function(change) {
console.log('changed', change)
})
} catch (err) {
console.log('mongo err:', err)
}
这很好用,但几分钟后它会经常失去连接错误:
未捕获的MongoNetworkError:连接6到localhost:27017超时
根据the documentation,它应该在出错时自动重新连接多达30次尝试,但它似乎没有进一步尝试重新连接。
我还需要在重新连接时运行一些额外的逻辑以正确处理本地状态。
我怎样才能抓住并处理这些错误?
编辑:虽然我还没有收到其他事件,但是在发生错误后我会收到“重新连接”事件。所以我似乎至少可以对这些错误作出反应,但实际上并没有抓住它们。
回答如下:只需添加连接keepAlive: true
,或特定时间keepAlive: 300000
请参阅此处的示例:https://ide.c9.io/ibrahimth/mongo
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://localhost:27017/?replicaSet=rs'
// Database Name
const dbName = 'test';
// create a client, passing in additional options
const client = new MongoClient(url, {
keepAlive: true,
connectTimeoutMS: 60000,
socketTimeoutMS: 60000,
});
// Use connect method to connect to the server
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected correctly to server");
const db = client.db(dbName);
createCollated(db, function() {
client.close();
});
});
function createCollated(db, callback) {
db.createCollection('acount',
{
'collation' :
{ 'acountnam': 'firstacount' }
},
function(err, results) {
console.log("Collection created.");
callback();
}
);
};
有关更多信息:http://mongodb.github.io/node-mongodb-native/3.0/tutorials/collations/