samedi 4 avril 2015

TypeError: object is not a function in using MongoDB with Node.js

I was going through question and it asked me calculate minimum value of score for each child in class. I wrote the following source code.

I am using Mongo 2.6.9 and Node v0.10.25 and 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" }
}
}
]);
});


This aggregate query when run using node app.js gives this error



/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)


Which according to me is coming from the aggregate function. But when i run the same query in mongo terminal i get a proper output.



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 }


Ques- What and where is the mistake and how to rectify it .

Thanks.


Aucun commentaire:

Enregistrer un commentaire