node.js - MongoDB CursorNotFound 数百条小记录的 collection.find() 错误
问题描述
我正在使用本机 Node JS 驱动程序(v. 3.0.10)在 Mongo 3.6.6(在小型 Mongo Atlas 集群上,未分片)上运行
我的代码如下所示:
const records = await collection.find({
userId: ObjectId(userId),
status: 'completed',
lastUpdated: {
$exists: true,
$gte: '2018-06-10T21:24:12.000Z'
}
}).toArray();
我偶尔会看到这个错误:
{
"name": "MongoError",
"message": "cursor id 16621292331349 not found",
"ok": 0,
"errmsg": "cursor id 16621292331349 not found",
"code": 43,
"codeName": "CursorNotFound",
"operationTime": "6581469650867978275",
"$clusterTime": {
"clusterTime": "6581469650867978275",
"signature": {
"hash": "aWuGeAxOib4XWr1AOoowQL8yBmQ=",
"keyId": "6547661618229018626"
}
}
}
最多返回几百条记录的查询会发生这种情况。每个记录有几百个字节。
我在网上查看了可能是什么问题,但我发现的大部分内容都是关于需要超过 10 分钟才能完成的非常大的操作的光标超时。我不能确切地知道失败的查询从我的日志中花费了多长时间,但最多只有两秒(可能比这短得多)。
我测试了使用与出错的查询相同的值运行查询,并且执行时间explain
仅为几毫秒:
"executionStats" : {
"executionSuccess" : true,
"nReturned" : NumberInt(248),
"executionTimeMillis" : NumberInt(3),
"totalKeysExamined" : NumberInt(741),
"totalDocsExamined" : NumberInt(741),
"executionStages" : {...}
},
"allPlansExecution" : []
]
}
有任何想法吗?间歇性网络延迟会导致此错误吗?我将如何减轻这种情况?谢谢
解决方案
您可以尝试以下 3 件事:
db.collection.find().noCursorTimeout();
您必须使用 cursor.close() 在某个点关闭光标;
b)或者减少批量大小
db.inventory.find().batchSize(10);
c) 游标过期时重试:
let processed = 0;
let updated = 0;
while(true) {
const cursor = db.snapshots.find().sort({ _id: 1 }).skip(processed);
try {
while (cursor.hasNext()) {
const doc = cursor.next();
++processed;
if (doc.stream && doc.roundedDate && !doc.sid) {
db.snapshots.update({
_id: doc._id
}, { $set: {
sid: `${ doc.stream.valueOf() }-${ doc.roundedDate }`
}});
++updated;
}
}
break; // Done processing all, exit outer loop
} catch (err) {
if (err.code !== 43) {
// Something else than a timeout went wrong. Abort loop.
throw err;
}
}
}
推荐阅读
- python-3.x - Python APScheduler 密钥错误
- python - 提供形状为 (?, n) 的占位符时出现问题
- r - 对于第三个预测变量的特定范围,我如何可视化来自 NN 的 2 个预测变量的洞察力
- html - 我的 CSS 过渡有问题,它在第一次运行时不起作用
- php - 在 mongodb PHP 中同时获取嵌套数组和更新字段
- asp.net-mvc - 在 React.JS 查看 ASP.NET MVC 中未定义的值
- python-3.x - BS4:AttributeError:“NoneType”对象没有属性“文本”
- python - pybind11 如何使用 setup.py 添加 C++ 依赖项
- c++ - 将 Boost.Python 与第三方绑定混合使用
- java - 参数化 JMeter HTTP 请求路径