数百条小记录的collection.find()上的MongoDB CursorNotFound错误 [英] MongoDB CursorNotFound Error on collection.find() for a few hundred small records
问题描述
我正在使用本机Node JS驱动程序(v.3.0.10)在Mongo 3.6.6(小型Mongo Atlas集群,未分片)上运行
I'm running on Mongo 3.6.6 (on a small Mongo Atlas cluster, not sharded) using the native Node JS driver (v. 3.0.10)
我的代码如下:
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"
}
}
}
对于最多返回几百条记录的查询,就是这种情况.每个记录有几百个字节.
This is happening for queries that return a few hundred records at most. The records are a few hundred bytes each.
我在网上寻找问题的根源,但最多内容的a>我
I looked online for what the issue might be but most of what I found is talking about cursor timeouts for very large operations that take longer than 10 minutes to complete. I can't tell exactly how long the failed queries took from my logs, but it's at most two seconds (probably much, much shorter than that).
我测试了使用与错误输出相同的值来运行查询,并且从explain
开始的执行时间只有几毫秒:
I tested running the query with the same values as one that errored out and the execution time from explain
was just a few milliseconds:
"executionStats" : {
"executionSuccess" : true,
"nReturned" : NumberInt(248),
"executionTimeMillis" : NumberInt(3),
"totalKeysExamined" : NumberInt(741),
"totalDocsExamined" : NumberInt(741),
"executionStages" : {...}
},
"allPlansExecution" : []
]
}
有什么想法吗?间歇性网络延迟会导致此错误吗?我该如何缓解呢?谢谢
Any ideas? Could intermittent network latency cause this error? How would I mitigate that? Thanks
推荐答案
您可以尝试以下三件事:
You can try these 3 things:
db.collection.find().noCursorTimeout();
您必须使用cursor.close()在某个点关闭光标
You must close the cursor at some point with cursor.close();
b)或减小批量大小
db.inventory.find().batchSize(10);
c)光标过期后重试:
c) Retry when the cursor expires:
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;
}
}
}
这篇关于数百条小记录的collection.find()上的MongoDB CursorNotFound错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!