mibbit
mibbit

Reputation: 5365

MongoDB cannot call method 'aggregate' of undefined

I am trying to pull the lowest score in an array. My error is cannot call method 'aggregate' of undefined. The aggregate method is defined in mongodb, I even foud it, and db.students is certainly defined. I can't seem to find where i am going wrong.

var MongoClient = require('mongodb').MongoClient // Driver for connecting to MongoDB

MongoClient.connect('mongodb://localhost:27017/school', function(err, db) {
      if(err) throw err;

x = db.students.aggregate( [{ "$unwind":"$scores" },{"$match":{"scores.type":"homework"}},{ "$group":{"_id":"$_id","minscore":{"$min":"$scores.score" }}} ] )

x.result.forEach(function(doc) {
   db.students.update( { "_id": doc._id }, 
                       { "$pull": { "scores" : { 
                                          "score":doc.minscore, 
                                          "type":"homework"
                       } } } 
   );
});

});

I have that feeling that I am getting tunnel vision from staring at this for too long, thanks for looking over it for me with a fresh set of eyes.

Upvotes: 1

Views: 4535

Answers (1)

EmptyArsenal
EmptyArsenal

Reputation: 7464

If you're going to use the node driver, you can't use the same syntax as you would in the Mongo shell. It should be like this:

db.collection('students').aggregate();

Here's the reference.

Upvotes: 1

Related Questions