title: How to call mongodb collection getall by node.js?
body: <p>I wanna take the whole list of notifies from mongo db but it returns empty([]) array also I know that i need callback or shorter way of it . Do you have any idea for collecting any data from mongodb by node.js? If I call this /Notifies method (<a href="http://127.0.0.1:5000/Notifies" rel="noreferrer">http://127.0.0.1:5000/Notifies</a>)</p>
  
  <pre><code>var MongoClient = require('mongodb').MongoClient;
  var express = require("express");
  var app = express();
  
  
  format = require('util').format;
  MongoClient.connect('mongodb://127.0.0.1:27017/Test', function (err, db) {
      if (err) {
          throw err;
      } else {
          console.log("successfully connected to the database");
      }
      db.close();
  });
  
  app.get('/Notifies', function (req, res) {
  
      // BAD! Creates a new connection pool for every request
      console.log('connected');
      MongoClient.connect('mongodb://127.0.0.1:27017/Test', function (err, db) {
          if (err) throw err;
  
          var coll = db.collection('Notifies');
          var arr = [];
          coll.find({}, function (err, docs) {
              docs.each(function (err, doc) {
                  if (doc) {
                      console.log(doc);
                      arr.push(doc);
  
                  } else {
                      res.end();
                  }
              });
          });
          return res.json(arr);
      });
  });
  
  var port = Number(process.env.PORT || 5000);
  app.listen(port, function () {
      console.log("Listening on " + port);
  })
  </code></pre>