Nodejs mongodb 驱动程序在客户端关闭时挂起
Nodejs mongodb driver hangs on client close
我有以下代码,无法理解为什么我的进程挂在试图关闭与 mongodb 的连接的线上,这是我的代码:
async function save(clientCredentials, newFieldsToUpdate){
const url = `mongodb://${clientCredentials.username}:${clientCredentials.password}@my.server.ip:22222/${clientCredentials.database}`
const client = await MongoClient.connect(url, {useNewUrlParser:true, useUnifiedTopology: true})
.catch(err => { console.log(err); });
const db = client.db(clientName);
const collection = await db.collection("products");
let execute = false;
const updateOps = [];
for(let objectIdentifier in newFieldsToUpdate){
let updateOperation = {};
updateOperation['$set'] = newFieldsToUpdate[objectIdentifier];
let id = mongodb.ObjectID(objectIdentifier);
execute = true;
updateOps.push({ updateOne: { filter: {_id: id}, update: {$set: newFieldsToUpdate[objectIdentifier]}, upsert:true } })
}
if(execute){
try {
console.log('executing'); // I see this line
let report = await collection.bulkWrite(updateOps);
console.log('executed'); // I see this line
await client.close();
console.log('closed conn'); // I don't see this line! why? it's weird
return report;
} catch(ex){
console.error(ex);
}
} else {
console.log('not executing');
}
}
提前感谢您的帮助!
编辑:批量操作仅涉及大约 200 个文档,如果我尝试使用单个文档,它会起作用,这很奇怪。 Mongodb 节点版本 3.3.2 的驱动程序
EDIT2:我注意到在 mongo 连接上使用参数 poolSize:1 它会成功关闭连接,但是使用默认的 poolSize 5 它不会关闭,任何建议为什么会这样发生了什么?
确保您使用的是最新版本的 mongodb
驱动程序(删除 node_modules 只是为了确定),因为旧版本有一个错误,其中 bulkWrite
方法由于某些原因静默失败此处提到的错误 -
在尝试关闭连接之前,您还应该检查它是否已连接。所以代码将是
if (client.isConnected) {
await client.close();
}
希望对您有所帮助
我有以下代码,无法理解为什么我的进程挂在试图关闭与 mongodb 的连接的线上,这是我的代码:
async function save(clientCredentials, newFieldsToUpdate){
const url = `mongodb://${clientCredentials.username}:${clientCredentials.password}@my.server.ip:22222/${clientCredentials.database}`
const client = await MongoClient.connect(url, {useNewUrlParser:true, useUnifiedTopology: true})
.catch(err => { console.log(err); });
const db = client.db(clientName);
const collection = await db.collection("products");
let execute = false;
const updateOps = [];
for(let objectIdentifier in newFieldsToUpdate){
let updateOperation = {};
updateOperation['$set'] = newFieldsToUpdate[objectIdentifier];
let id = mongodb.ObjectID(objectIdentifier);
execute = true;
updateOps.push({ updateOne: { filter: {_id: id}, update: {$set: newFieldsToUpdate[objectIdentifier]}, upsert:true } })
}
if(execute){
try {
console.log('executing'); // I see this line
let report = await collection.bulkWrite(updateOps);
console.log('executed'); // I see this line
await client.close();
console.log('closed conn'); // I don't see this line! why? it's weird
return report;
} catch(ex){
console.error(ex);
}
} else {
console.log('not executing');
}
}
提前感谢您的帮助!
编辑:批量操作仅涉及大约 200 个文档,如果我尝试使用单个文档,它会起作用,这很奇怪。 Mongodb 节点版本 3.3.2 的驱动程序
EDIT2:我注意到在 mongo 连接上使用参数 poolSize:1 它会成功关闭连接,但是使用默认的 poolSize 5 它不会关闭,任何建议为什么会这样发生了什么?
确保您使用的是最新版本的 mongodb
驱动程序(删除 node_modules 只是为了确定),因为旧版本有一个错误,其中 bulkWrite
方法由于某些原因静默失败此处提到的错误 -
在尝试关闭连接之前,您还应该检查它是否已连接。所以代码将是
if (client.isConnected) {
await client.close();
}
希望对您有所帮助