title: Meteor Mongo Collections find forEach cursor iteration and saving to ElasticSearch Problem
body: <p>i have Meteor App which is connected to MongoDB.<br />
  In mongo i have a table which has ~700k records.
  I have a cron job each week, where i read all the records from the table (using Mongo Cursor) and in batches of 10k i want to insert them inside Elastic Search so they are indexed.</p>
  <pre><code>let articles = []
  Collections.Articles.find({}).forEach(function(doc) {
          articles.push({ 
              index: {_index: 'main', _type: 'article', _id: doc.id }
          },
          doc);
          if (0 === articles.length % 10000) {
            client.bulk({   maxRetries: 5,  index: 'main',   type: 'article',    body: articles   })
            data = []
          }
      })
  </code></pre>
  <p>Since for each is synchronous, goes over each record before it continues, and client.bulk is async, this is overloading the elastic search server and it crashes with Out of Memory Exception.<br />
  Is there a way to pause the forEach during the time when the insert is being done? I tried async/await but this does not seem to work as well.</p>
  <pre><code>let articles = []
  Collections.Articles.find({}).forEach(async function(doc) {
          articles.push({ 
              index: {_index: 'main', _type: 'article', _id: doc.id }
          },
          doc);
          if (0 === articles.length % 10000) {
            await client.bulk({   maxRetries: 5,  index: 'main',   type: 'article',    body: articles   })
            data = []
          }
      })
  </code></pre>
  <p>Any way how to achieve this?</p>
  <p>EDIT: I am trying to achieve something like this - if i use promises</p>
  <pre><code>let articles = []
  Collections.Articles.find({}).forEach(function(doc) {
          articles.push({ 
              index: {_index: 'main', _type: 'article', _id: doc.id }
          },
          doc);
          if (0 === articles.length % 10000) {
          // Pause FETCHING rows with forEach
              client.bulk({   maxRetries: 5,  index: 'main',   type: 'article',    body: articles    }).then(() =&gt; {
                                            console.log('inserted')
                                            // RESUME FETCHING rows with forEach
                                            console.log(&quot;RESUME READING&quot;);  
              })
            data = []
          }
      })
  </code></pre>