Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You might want to try the <a href="https://github.com/caolan/async" rel="nofollow">async</a> module for this. It has some very helpful methods for processing each item in a collection, and offers functionality for when all processing has finished. </p> <p>I refer you in particular to the <a href="https://github.com/caolan/async#queue" rel="nofollow">queue</a> function, which lets you add tasks to a queue, then once all items have been processed, do something. </p> <p>For instance, you might do something like:</p> <pre><code>var q = async.queue(function(task, callback) { // task.doc would contain your individual doc to process // your insert / update logic goes here... // Callback signifies you're done with this task callback(); }, 2) // &lt;--- this number specifies the number of tasks to run in parallel q.drain = function() { // this is the queue's callback, called when the queue is empty, // i.e. when all your documents have been processed. res.send(statusCode, message); } </code></pre> <p>Then, if we assume you have your list of documents in a variable named <strong>docs</strong>, all you need to do to process them all is push them onto the queue.</p> <pre><code>for (var doc in docs) { q.push({ doc: docs[doc] }, function(err) { if (err) console.log(err); }) } </code></pre> <p>Protip: you need to push an object <em>containing</em> the document onto the queue. There's a weird error if you try to pass in an unwrapped object.</p> <p>Now, if you wanted the specific statuses for each document you process in Mongo, this is totally possible like this. As long as you instantiated a data structure outside the queue, you could add statusCodes (etc.) to it as each item is processed, and send the structure to the client in the queue's <em>drain</em> function. Shouldn't be too much hassle.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload