gpt4 book ai didi

node.js - 类型错误 : object is not a function in using MongoDB with Node. js

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:52 25 4
gpt4 key购买 nike

我正在回答一个问题,它要求我计算类每个 child 的最低分数。我写了下面的源代码。
我正在使用 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" }
}
}
]);
});

使用 Node 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)

据我所知,这是来自聚合函数。但是当我在 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 }

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

最佳答案

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

关于node.js - 类型错误 : object is not a function in using MongoDB with Node. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29451796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com