【发布时间】:2014-02-04 04:00:46
【问题描述】:
我正在尝试获取数组中的最低分数。我的错误是无法调用未定义的方法“聚合”。聚合方法是在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"
} } }
);
});
});
我有一种感觉,我盯着这个看太久了,我有一种狭隘的感觉,感谢你用全新的眼光为我看它。
【问题讨论】:
-
db.students 不是有效对象。您需要通过 db.collection('students').aggregate(...) 访问该集合
标签: node.js mongodb aggregation-framework