title: Node.js - Counting the number of objects in MongoDB collection
body: <p>I am new to Node.js and MongoDB and I am really struggling to wrap my head around callbacks. I have read a few articles but it is still quite confusing to me. In the code below, I am trying to return the count of <code>orders</code> that have some properties which I have expressed in the query in <code>orderModel.count(query, next)</code>:</p>
  
  <p>controllers/order.js:</p>
  
  <pre><code>var mongoose = require ('../config/db');
  var orderModel = require('../models/order').model;
  var User = require('./user');
  var Error = require('../config/error');
  createOrder: function (user, order, next) {
    if (newOrder.totalPrice &gt; user.credit &amp;&amp; orderModel.
              count({$and: [{user: order.user}, {active: true}, {$or: [{status: 0}, {status: 1}]}]},
               function(err, count){
                 if(err)
                  console.log(err);
                 else
                   count; }) &gt; 0)
                return next({error: Error.InsufficientCredits});
  }
  </code></pre>
  
  <p>I don't think I am correctly obtaining the variable <code>count</code> because when I tried printing out the result of the second condition in the <code>if</code> statement, I got this data printed out:</p>
  
  <pre><code>Query {
  _mongooseOptions: {},
  
  
  mongooseCollection: 
     NativeCollection {
       collection: Collection { s: [Object] },
       opts: { bufferCommands: true, capped: false },
       name: 'orders',
       collectionName: 'orders',
       conn: 
        NativeConnection {
          base: [Object],
          collections: [Object],
          models: [Object],
          config: [Object],
          replica: false,
          hosts: null,
          host: 'dsXXXXXX.mlab.com',
          port: XXXXXX,
          user: 'XXXX',
          pass: 'XXXX',
          name: 'X',
          options: [Object],
          otherDbs: [],
          _readyState: 1,
          _closeCalled: false,
          _hasOpened: true,
          _listening: false,
          db: [Object],
          _events: {},
          _eventsCount: 0 },
       queue: [],
       buffer: false,
       emitter: 
        EventEmitter {
          domain: null,
          _events: {},
          _eventsCount: 0,
          _maxListeners: undefined } },
  ....
  </code></pre>