2

I'm trying to query a collection using aggregate but getting TypeError: users.aggregate is not a function. Here's my code:

./app.js (reduced to fit)

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;

./routes/users.js

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;

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 req.db.collection('users').aggregate (as suggested by another post) and receive a similar error: req.db.collection is not a function. What am I missing?

EDIT: Running users.find({ username: 'test0' }, func... works and returns correct data.

EDIT2: Added more code.

4
  • Where from req.db.get? Are you exporting that? Commented Nov 1, 2016 at 1:51
  • what library are you using to connect to mongodb? Commented Nov 1, 2016 at 2:46
  • @QoP I'm using monk Commented Nov 1, 2016 at 3:31
  • Deeper searching found that monk is incompatible with aggregate and I must use users.col.aggregate(). Answered here: stackoverflow.com/questions/23951123/… Commented Nov 2, 2016 at 2:32

2 Answers 2

1

You can do like this.

var app = require('express')();
var expressMongoDb = require('express-mongo-db');

app.use(expressMongoDb('mongodb://localhost/test'));

app.get('/', function (req, res, next) {
  req.db // => Db object
});
Sign up to request clarification or add additional context in comments.

1 Comment

I'm returning the database just fine in my req.db.get('users'). I'm able to find(), findOne(), update(), and insert() to it, but aggregate is failing.
0

I tried with this. It is working in my local.

var app = require('express')();

const db = require('monk')('localhost/test')

app.use(function(req,res,next){
 req.db = db;
 next();
});

app.get('/', function (req, res, next) {
  var db = req.db;
  var users = db.get('users');

  users.aggregate([
   {$match: { firstName: 'Test 2' }}
    ]).then(function(docs) {
    console.log(docs)
    })
 });

 app.listen(3010, function () {
  console.log('Example app listening on port 3010!');
 });

1 Comment

Thanks for your time, but I found an answer after specifically searching for monk library issues with aggregate. It turns out, it's not supported and I had to use the users collection with users.col.aggregate()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.