I’m using the latest mongodb driver for nodejs 3.5.3 but I’m having issues with cursors.
I’m planning on processing a table of 450k+ rows and doing some async operations so obviously I won’t want to use toArray() first.
-
A simple
cursor.forEach
isn’t working – the async function isn’t being called at all.const cursor = client.db().collection('properties').find().limit(15); cursor.forEach( async function(row){ return knex('properties').insert(row).then(console.log).catch(console.error); } ,async function(err){ if(err) console.error(err); await client.close(); console.log('done'); process.exit(); })
-
When I try simply using Bluebird’s Promise.map (which should handle async and concurrency), I get UnhandledPromiseRejectionWarning: TypeError: expecting an array or an iterable object but got [object Null]
-
When I use npm: mongo-iterable-cursor to convert the cursor, I get the same error with Bluebird. (maybe that’s for driver v2?)
-
When I use a native iterator code found on stack exchange (javascript - Async Cursor Iteration with Asynchronous Sub-task - Stack Overflow)
for await ( let row of cursor ) { console.log(row._id) return Promise.delay(2000); }
We never get to the second row.
-
Similar with this code, we never get to the second row: node.js - Iterating over a mongodb cursor serially (waiting for callbacks before moving to next document) - Stack Overflow
while(await cursor.hasNext()) { const row = await cursor.next(); console.log(row._id); return Promise.delay(2000); }
(The Promise.delay is the same as my issues with calling knex with DB commands)
Besides for the cursor simply not working, I want to use bluebird for processing the cursor since it has a concurrency setting. I’ve never seen an explanation of how cursor.forEach handled awaits.
What am I doing wrong? Suggestions? Thanks!