title: How to execute mongoose queries immediately and wait for them with Promise.all?
body: <p>I'm just getting used to promises and I want to add an additional ObjectId reference to a table. I use this code:</p>
  
  <pre><code>'use strict';
  
  const mongoose = require('mongoose');
  const config = require('config');
  
  mongoose.connect(config.get("DBUrl"), {useMongoClient: true});
  mongoose.set('debug', true);
  
  require('../model/AnnotationRequest');
  require('../model/Website');
  
  const AnnotationRequest = mongoose.model('AnnotationRequest');
  const Website = mongoose.model('Website');
  
  foo(function () {
      console.log('done');
  });
  
  function foo(done) {
  
      Website.find()
          .then(function (websites) {
              if (!websites) console.error('could not find any websites');
  
              AnnotationRequest.find({website: {$exists: false}})
                  .then(function (annotationRequests) {
  
                      let jobs = [];
  
                      for (let k = 0; k &lt; annotationRequests.length; k++) {
                          let found = false;
  
                          for (let i = 0; i &lt; websites.length; i++) {
                              let websiteAnnotations = websites[i].annotations;
  
                              for (let o = 0; o &lt; websiteAnnotations.length; o++) {
  
                                  if (websiteAnnotations[o].equals(annotationRequests[k].annotation)) {
                                      found = true;
                                      annotationRequests[k].website = websites[i];
                                      jobs.push(
                                          annotationRequests[k].save()
                                              .then(function (res) {
                                                  console.log("success saving", res._id);
                                              }).catch(function (err) {
                                                  console.error(err);
                                                  done(err);
                                              })
                                      );
                                      break;
                                  }
                              }
                              if (found) {
                                  break;
                              }
                          }
                      }
  
                      Promise.all(jobs).then(
                          function () {
                              done();
                          }
                      ).catch(function (err) {
                          console.error(err);
                      });
                  });
          });
  }
  </code></pre>
  
  <p>The first save query is reported to be started at the Promise.all statement. Should the queries not be executed immediately after the first <code>.save()</code> statement? How can I start them immediately?</p>
  
  <p>The other Problem is that with 200k entries this code takes 2 hours. On the server we have > 2M entries. Not sure if it will work. Any suggestions?</p>
  
  <p>Thanks</p>