title: MongoDB - TypeError: users.aggregate is not a function
body: <p>I'm trying to query a collection using <code>aggregate</code> but getting <code>TypeError: users.aggregate is not a function</code>. Here's my code:</p>
  
  <p><strong>./app.js</strong> (reduced to fit)</p>
  
  <pre><code>var express = require('express');
  var mongo = require('mongodb');
  var monk = require('monk');
  var db = monk('localhost:27017/mydb');
  var users = require('./routes/users');
  var app = express();
  
  app.use(function(req, res, next) {
      req.db = db;
      next();
  });
  
  app.use('/users', users);
  module.exports = app;
  </code></pre>
  
  <p><strong>./routes/users.js</strong></p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  
  router.get('/test', function(req, res) {
    var users = req.db.get('users');
  
    users.aggregate([{ $match: { username: 'test0' }}], function(err, data) {
      res.json(data);
    });
  });
  
  module.exports = router;
  </code></pre>
  
  <p>I'm running MongoDB version 3.2.10 and the above query works fine in the console. I've looked up solutions and they all suggest to check the MongoDB version, which I've already done. I've also tried <code>req.db.collection('users').aggregate</code> (as suggested by another post) and receive a similar error: <code>req.db.collection is not a function</code>. What am I missing?</p>
  
  <p>EDIT:
  Running <code>users.find({ username: 'test0' }, func...</code> works and returns correct data.</p>
  
  <p>EDIT2: Added more code.</p>