I'm trying to fetch large data from mongodb
There is my code
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")
Output:
Checkpoint one
Checkpoint two
Execution ended. Number of elements: 2194.
Expected output:
Checkpoint one
Execution ended. Number of elements: 2194.
Checkpoint two
When I'm trying to console log each res inside "on data" function, its also console logging after "Checkpoint two",