title: MongoDB - How can I get the size of a collection using Node.js?
body: <p>I'm writing a server side code using node.js, I'm trying to get the MongoDB collection size using count method which is not working.</p>
  
  <p>This is my code</p>
  
  <pre><code>var mongo = require('mongodb');
  var host =  "127.0.0.1";
  var port = mongo.Connection.DEFAULT_PORT;
  
  function getStock(name, callback) {
  
      var db = new mongo.Db("testDB", new mongo.Server(host, port, {}));
      db.open (function(error){
          console.log("We are connected! " + host + ":" +port);
  
          db.collection("stocks", function(error, collection){
              console.log("We have a collection");
              **var numOfDocs = db.collection('stocks').count()**
              **console.log("The num of  Docs in our collection is: ",numOfDocs)**
              collection.find({"name":name.toString()}, function(error, cursor) {
                  cursor.toArray(function(error, stocks) {
                      if (stocks.length==0) {
                          //console.log("No Stocks found!");
                          callback(false);
                      }
                      else {
                          callback(stocks[0]);
                          //console.log("Found a stock -&gt; ",stocks[0]);
                      }
                  });
              });
  
  
          });
      });
  }
  </code></pre>