jmd*_*ego 3 mongodb node.js database-cursor
我正在对光标的每个元素执行一个进程,我的集合有大约 600 万个文档,整个过程最多需要 10 个小时,因为我必须一次处理一个配置文件,而且对于每一个。
\n\n var cursor = dbMain.collection("profiles").find({});\n\n var getNext = function(){\n cursor.nextObject(processOne);\n };\n\n var processOne = function(err, profile){\n if(err){\n console.error("Error loading profile", err);\n getNext();\n } else if(profile == null){\n // process is done, all profiles processed!!!\n } else {\n processProfile(profile)\n .then(function(){\n getNext();\n })\n .catch(function(errProfile){\n console.error("Error processing profile", errProfile);\n getNext();\n });\n }\n };\n\n var processProfile = function(profile){\n // Complex profile processing and promise resolve when done\n };\n\n getNext();\nRun Code Online (Sandbox Code Playgroud)\n\n问题:当我有大约 300,000 个配置文件时,我得到 null(这意味着光标已耗尽),因此,只有前 300,000 个左右得到处理。\xc2\xbf有人知道如何处理它或者我提前得到 null 的原因吗?
\n对于任何在最新 MongoDB 驱动程序上偶然发现此驱动程序的人,请注意以下事项。如果您.hasNext()在上次.next()调用后调用两次,您将得到:Error: Cursor is closed。
例如:
while(await cursor.hasNext()) {
await cursor.next();
}
if (await cursor.hasNext()) {} // Error!
if (!cursor.isClosed() && await cursor.hasNext()) {} // Correct way
Run Code Online (Sandbox Code Playgroud)
看来我已经找到原因了:
由于它是一个需要存活很长时间的游标,因此在查找选项中我必须添加“timeout : false”。
var cursor = dbMain.collection("profiles").find({}, {timeout : false});
Run Code Online (Sandbox Code Playgroud)
在驱动程序文档中说默认情况下它是错误的(其实不是!!),现在我的所有配置文件都得到处理。
| 归档时间: |
|
| 查看次数: |
3732 次 |
| 最近记录: |