title: db.collection.find() How to get the number of Documents?
body: <p>I am trying to build a basic app using Node.js/Heroku with MongoDB/mLab, where a record is supposed to be added to a collection each time it is run. Here is the relevant code:</p>
  
  <pre><code>.........
  
  app.get('/', function(req, res) {
    //res.status(200).send('I dream of being a website.  Please star the parse-server repo on GitHub!');
    topDisplayFunc("index", res);
  });
  
  .........
  
  
  function topDisplayFunc(displayPage, response) {
    MongoClient.connect(databaseUri, function(err, db) {
      if (err) throw err;
  
      db.collection('XCollection', function (err, collection) {
        collection.find().toArray(function(err, items) {
          if(err) throw err;
  
          response.render('pages/' + displayPage, {
            dataArray: items
          });
  
          collection.insert({number: collection.count}, function (err) {
            if(err) throw err;
            db.close();
          });
        });
      });
    })  ;
  }
  </code></pre>
  
  <p>I start by removing XCollection from the list of collections.</p>
  
  <p>The first time the page is visited XCollection is created with a record inside, XCollection then contains one document looking like this:</p>
  
  <pre><code>{
      "_id": {
          "$oid": "544e6f91d22e2e001171d117"
      }
  }
  </code></pre>
  
  <p>The second time it is visited, another record is created, XCollection then contains one more document:</p>
  
  <pre><code>{
      "_id": {
          "$oid": "544e6f92e22e2e001171d118"
      }
  }
  </code></pre>
  
  <p>As one can see, the field number does not appear.</p>
  
  <p>If I change this line of code:</p>
  
  <pre><code>collection.insert({number: collection.count}, function (err) {
  </code></pre>
  
  <p>to this one:</p>
  
  <pre><code>collection.insert({number: collection.count()}, function (err) {
  </code></pre>
  
  <p>Then the added documents change look to something like:</p>
  
  <pre><code>{
      "_id": {
          "$oid": "544e6f93e22e2e001171d119"
      },
      "number": {}
  }
  </code></pre>
  
  <p>This time the field number appears, but with no value.</p>
  
  <p>The code should add a document containing 0 the first time it is run, 1 the next time, then 2, then 3 ...etc...
  But it does not work. Only some garbage is added each time in the newly created document. What should I change?
  I need to say that all I have tried fails.</p>