TypeError:对象不是将MongoDB与Node.js一起使用的函数

Sar*_*rya 1 mongodb node.js

我正在经历一个问题,它要求我计算每个孩子在课堂上得分的最低值.我写了以下源代码.
我使用的是Mongo 2.6.9和Node v0.10.25以及ubuntu 14.04 LTS

var MongoClient=require('mongodb').MongoClient;
var server=require('mongodb').Server;

var mongoclient=new MongoClient(new server("localhost",27017));

mongoclient.connect("mongodb://localhost:27017/",function(err,db){
if(err) throw err;

var db=mongoclient.db('school');

cursor=db.collection('students').aggregate(
[
  {$match : {"scores.type" : "homework"}},
  {$unwind:"$scores"},
  {$group : {_id : '$name',
  'minimum' : { $min :"$scores.score"  }
}
}
]);
});
Run Code Online (Sandbox Code Playgroud)

使用节点app.js运行时,此聚合查询会出现此错误

/home/oroborus/node_modules/mongodb/lib/mongodb/connection/base.js:246
        throw message;      
              ^
TypeError: object is not a function
    at /home/oroborus/node_modules/mongodb/lib/mongodb/collection/aggregation.js:317:7
    at /home/oroborus/node_modules/mongodb/lib/mongodb/db.js:1195:7
    at /home/oroborus/node_modules/mongodb/lib/mongodb/db.js:1903:9
    at Server.Base._callHandler (/home/oroborus/node_modules/mongodb/lib/mongodb/connection/base.js:453:41)
    at /home/oroborus/node_modules/mongodb/lib/mongodb/connection/server.js:488:18
    at MongoReply.parseBody (/home/oroborus/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
    at null.<anonymous> (/home/oroborus/node_modules/mongodb/lib/mongodb/connection/server.js:446:20)
    at EventEmitter.emit (events.js:95:17)
    at null.<anonymous> (/home/oroborus/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:207:13)
    at EventEmitter.emit (events.js:98:17)
Run Code Online (Sandbox Code Playgroud)

根据我的说法来自聚合函数.但是当我在mongo终端中运行相同的查询时,我得到了正确的输出.

cursor=db.students.aggregate( [   {$match : {"scores.type" : "homework"}},   {$unwind:"$scores"},   {$group : {_id : '$name', 'minimum' : {   $min :"$scores.score"  } } } ]);
{ "_id" : "Myrtle Wolfinger", "minimum" : 35.99397009906073 }
{ "_id" : "Gennie Ratner", "minimum" : 34.50565589246531 }
{ "_id" : "Nobuko Linzey", "minimum" : 19.27081566886746 }
{ "_id" : "Flora Duell", "minimum" : 40.68238966626067 }
{ "_id" : "Shin Allbright", "minimum" : 52.68629677727286 }
Run Code Online (Sandbox Code Playgroud)

问题 - 错误是什么以及在哪里以及如何纠正错误.
谢谢.

Ben*_*Ben 5

collection.aggregate()的最后一个参数需要是回调.mongodb驱动程序期待一个函数,但你的最后一个参数是一个对象.这就是你得到这个错误的原因.以下是带有回调的修订代码:

var MongoClient = require('mongodb').MongoClient;
var server = require('mongodb').Server;

var mongoclient = new MongoClient(new server("localhost", 27017));

mongoclient.connect("mongodb://localhost:27017/", function(err, db) {
  if (err) throw err;

  var db = mongoclient.db('school');

  cursor = db.collection('students').aggregate(
    [
        {$match: {"scores.type": "homework"}},
        {$unwind: "$scores"},
        {
            $group: {
                _id: '$name',
                'minimum': {$min: "$scores.score"}
            }
        }
    ], function(err, result) {   // callback
        console.dir(result);
        db.close();
    }
  );
});
Run Code Online (Sandbox Code Playgroud)