title: Mongoose - How to save results to an array
body: <p>I have a mongodb query and I want to save the results to an array. I also define an empty array outside of query.</p>
  
  <pre><code>       var dietaryResults = [];
  
         for (var key in dietary){
              Restaurant.find(
                  { $text : { $search : dietary[key] } },
                  { score : { $meta: "textScore" } }
              ).sort({ score : { $meta : 'textScore' } }).exec(function(err, results) {
                  for (var i in results){
                      dietaryResults.push(results[i]);
                  }
                  console.log(dietaryResults);
              });
          }
  </code></pre>
  
  <p>If I do <code>console.log(dietaryResults)</code> inside the query like above, I can see the results being pushed into the array. But if I put <code>console.log(dietaryResults)</code> outside of the array (which what I want), it prints an empty string. Can anyone explain this behavior and suggest a solution for this? Thanks.</p>