It's a continuation of question from here . My aim is to update all the documents with minimum score.
var MongoClient=require('mongodb').MongoClient;
var server=require('mongodb').Server;
var mongoclient=new MongoClient(new server("localhost",27017));
mongoclient.connect("mongodb://localhost:27017/school",function(err,db){
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);
}
);
cursor.each(function(err,doc)
{
db.collection('students').update({'_id':doc._id},{$pull:{'scores':{'score':doc.minimum}}});
});
});
Using this i am getting error
node app.js
undefined
/home/oroborus/node_modules/mongodb/lib/mongodb/mongo_client.js:475
throw err
^
TypeError: Cannot call method 'each' of undefined
at /home/oroborus/hw3-1/app.js:18:8
at /home/oroborus/node_modules/mongodb/lib/mongodb/mongo_client.js:83:5
at /home/oroborus/node_modules/mongodb/lib/mongodb/mongo_client.js:472:11
at process._tickCallback (node.js:415:13)
According to previous programs written by me and the post above this looks correct then still why does this error persists ?
Thanks
Aucun commentaire:
Enregistrer un commentaire