title: How to get data from a mongoose aggregate query in nodejs
body: <p>The mongoose query is fetching the correct results but i am unable to access the data.</p>
  
  <p>I want to get the current max id in the database, increment the value by 1 and save the data to the database </p>
  
  <pre><code>// add a new book
  router.route('/books/add').post(jsonParser, (req, res) =&gt; {
  
  Book.aggregate([{ $group: { _id: "id", id: { $max: "$id" } } }], (err, books) =&gt; {
      if (!books)
          return next(new Error("Could not load book!"))
      else {
          let book = new Book(req.body);
  
          // log the max id returned, currently returns undefined
          console.log("Id found  ", books.id);
  
          book.id = books.id + 1;
          console.log("BookId ", book.id);
  
          res.status(200).json({ 'book': 'Added successfully!' });
          // book.save().then(book =&gt; {
          //     res.status(200).json({ 'book': 'Added successfully!' });
          // }).catch(err =&gt; {
          //     res.status(400).send('Failed to create new record.');
  
          // });
      }
  });
  </code></pre>
  
  <p>});</p>
  
  <p><code>console.log(books.id)</code> returns <code>undefined</code> whereas <code>console.log(books)</code> does show me the result which is <code>[ { _id: 'id', id: 5 } ]</code>. What i want to do is get the id i.e. 5, how do i do that ? Thank you. </p>