【发布时间】:2018-12-20 20:26:42
【问题描述】:
我正在尝试从 mongodb 获取大数据
这是我的代码
let cont = 0
const stream = await dataModel.find({}).lean().cursor(); // it will return around 2.000 elements
console.log("Checkpoint one")
await stream.on('data', async (res) => {
try {
cont += 1
} catch (e) {
console.log(e)
}
});
await stream.on('close', () => {
console.log(`Execution ended. Number of elements: ${cont}.`);
});
console.log("Checkpoint two")
输出:
Checkpoint one
Checkpoint two
Execution ended. Number of elements: 2194.
预期输出:
Checkpoint one
Execution ended. Number of elements: 2194.
Checkpoint two
当我尝试在“on data”函数中对每个 res 进行控制台日志记录时,它也在“检查点二”之后进行控制台日志记录,
【问题讨论】: