MongoDB无法调用未定义的方法'aggregate'

时间:2014-02-03 19:53:07

标签: node.js mongodb aggregation-framework

我试图在数组中获得最低分。我的错误是无法调用未定义的方法'aggregate'。聚合方法在mongodb中定义,我甚至对它进行定义,db.students肯定是定义的。我似乎无法找到我出错的地方。

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"
                       } } } 
   );
});

});

我有这样的感觉,我从长时间盯着它看隧道视觉,谢谢你用一双新鲜的眼睛为我寻找它。

1 个答案:

答案 0 :(得分:1)

如果您要使用节点驱动程序,则不能使用与Mongo shell中相同的语法。它应该是这样的:

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

Here's the reference.