asyncfunctiontest() { const members = await findAllMembers(); let N = 0; awaitPromise.mapSeries(members, member => { N++; console.log(`name of the ${N}th member: ${member.name}`); }); console.log(`loop all ${N} members success`); }
当 Member 集合中的 document 不多时,比如只有 1000 个时,那确实没有问题。但是当 Member 集合中有 1000 万个 document 时,会发生什么呢?如下:
<--- Last few GCs ---> rt of marking 1770 ms) (average mu = 0.168, current mu = 0.025) finalize [5887:0x43127d0] 33672 ms: Mark-sweep 1398.3 (1425.2) -> 1398.0 (1425.7) MB, 1772.0 / 0.0 ms (+ 0.1 ms in12 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 1775 ms) (average mu = 0.088, current mu = 0.002) finalize [5887:0x43127d0] 35172 ms: Mark-sweep 1398.5 (1425.7) -> 1398.4 (1428.7) MB, 1496.7 / 0.0 ms (average mu = 0.049, current mu = 0.002) allocation failure scavenge might not succeed
asyncfunctiontest() { const membersCursor = await findAllMembersCursor(); let N = 0; await membersCursor.eachAsync(member => { N++; console.log(`name of the ${N}th member: ${member.name}`); }); console.log(`loop all ${N} members success`); }
A QueryCursor is a concurrency primitive for processing query results one document at a time. A QueryCursor fulfills the Node.js streams3 API, in addition to several other mechanisms for loading documents from MongoDB one at a time.