title: How can I get the result of MongoDB's collection.aggregate() in NodeJS callback?
body: <p>I have the following <code>users</code> collection:</p>
  
  <pre class="lang-json prettyprint-override"><code>{
      "_id" : ObjectId("5b29ba37cd0b1726068731c3"),
      "name" : "Gym Dog",
      "profilePicUrl" : "https://i.imgur.com/mPStaKV.png",
      "totalProgress" : {
      "goal" : 300,
      "progress" : 0,
      "percentage" : 0
      },
      "goals" : [
      {
          "description" : "Running",
          "goal" : 100,
          "progress" : 0,
          "percentage" : 0
      },
      {
          "description" : "Lifting",
          "goal" : 200,
          "progress" : 0,
          "percentage" : 0
      }
      ],
      "days" : [
      {
          "weekday" : "Sunday",
          "content" : ""
      },
      {
          "weekday" : "Monday",
          "content" : ""
      },
      {
          "weekday" : "Tuesday",
          "content" : ""
      },
      {
          "weekday" : "Wednesday",
          "content" : ""
      },
      {
          "weekday" : "Thursday",
          "content" : ""
      },
      {
          "weekday" : "Friday",
          "content" : ""
      },
      {
          "weekday" : "Saturday",
          "content" : ""
      }
      ],
      "activities" : [ ]
  }
  </code></pre>
  
  <p><strong>I would like to select the 2nd element of <code>goals</code> of the user <code>Gym Dog</code> and then return the result in a NodeJS callback function.</strong></p>
  
  <hr>
  
  <p>Below is what I did in the <strong>Mongo shell</strong>:</p>
  
  <pre class="lang-js prettyprint-override"><code>db.users.aggregate(
      { $match: { name: 'Gym Dog' }},
      { $project: {
          _id: 0,
          ithGoal: { $arrayElemAt: [ '$goals', 1 ] }
      }}
  ).pretty()
  </code></pre>
  
  <p>Result from Mongo shell. <strong>This is also what I had expected my NodeJS code to output</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>{
      "ithGoal" : {
          "description" : "Lifting",
          "goal" : 200,
          "progress" : 0,
          "percentage" : 0
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Below is what I did in <strong>NodeJs</strong>:</p>
  
  <pre class="lang-js prettyprint-override"><code>mongoDB.collection('users').aggregate(
  // pipeline
  [
      { $match: { name: 'Gym Dog' }},
      { $project: {
          _id: 0,
          ithGoal: { $arrayElemAt: [ '$goals', 1 ] }
      }}
  ],
  // callback
  function(err, result) {
      if (err) {
          throw err
      }
      console.log(result);
  });
  </code></pre>
  
  <p>However, when <code>result</code> was logged into the console, it was an <code>AggregationCursor {}</code> object with a bunch of irrelevant info. It wasn't anything similar to the output from the Mongo shell (I don't include the output here because it's too long).</p>
  
  <p>I tried <code>mongoDB.collection('users').aggregate().toArray(function(err, result) {});</code> too, but it output the entire JSON object with <code>name</code>, <code>profilePicUrl</code>, <code>totalProgress</code>, <code>goals</code>, <code>days</code>, <code>activitise</code> (in other words, it selected nothing from the conditions I gave).</p>
  
  <p>So, my question is, <strong>how can I get the callback function in NodeJS to store the exact same result as the Mongo shell's into the <code>result</code> parameter?</strong></p>
  
  <p>I'm using MongoDB Community 3.6.5. Thanks in advance!</p>