游标没有下一个方法

cursor has no method next

我正在尝试遍历一个集合并为每个文档更新一个数组。 我在下面做错了什么?

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

var removeLowestHWScore = function(scores) {
  var lowestHWID = -1;
  for(var i=0;i<scores.length; i++) {
    if (scores[i].type === 'homework') {
      if (lowestHWID === -1) {
        lowestHWID = i;
      } else if (scores[i].score < scores[lowestHWID].score) {
        lowestHWID = i;
      }
    }
  }
  scores.splice(lowestHWID);
  return scores;
};

var callback = function(err, r) {
  console.log('updated record');
};

var updateScore = function(err, doc) {
  var updatedScores = removeLowestHWScore(doc.scores);
  collection.updateOne({_id:doc._id},
                       {$set: {scores: updatedScores }},
                       callback);
};

MongoClient.connect('mongodb://localhost:27017/school', function(err, db) {
  if(err) throw err;

  var collection = db.collection('students');
  var cursor = collection.find({});

  var next = cursor.next();
  while (next) {
    next(updateScore);
    next = cursor.next();
  }
  db.close();
});

错误

/Users/harrymoreno/programming/mongodb/mongo101ForNode/node_modules/mongodb

/lib/mongodb/mongo_client.js:475
      throw err
            ^
TypeError: Object #<Cursor> has no method 'next'
at     /Users/harrymoreno/programming/mongodb/mongo101ForNode/week03/app.js:35:21
at     /Users/harrymoreno/programming/mongodb/mongo101ForNode/node_modules/mongodb/lib/mongodb/mongo_client.js:472:11
at process._tickCallback (node.js:419:13)

样本学生

{
    "_id" : 137,
    "name" : "Tamika Schildgen",
    "scores" : [
        {
            "type" : "exam",
            "score" : 4.433956226109692
        },
        {
            "type" : "quiz",
            "score" : 65.50313785402548
        },
        {
            "type" : "homework",
            "score" : 89.5950384993947
        }
    ]
}

已更新 - v.2

根据您在上次评论中提供的有关 mongodb 软件包版本的信息,我已将解决方案更改为您为特定版本合规性而改进的解决方案(使用 1. 4.x node.js mongodb driver) :

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

var cursor = null,
    collection = null,
    dbSrc = null;

var removeLowestHWScore = function(scores) {
  var lowestHWID = -1;
  for(var i=0;i<scores.length; i++) {
    if (scores[i].type === 'homework') {
      if (lowestHWID === -1) {
        lowestHWID = i;
      } else if (scores[i].score < scores[lowestHWID].score) {
        lowestHWID = i;
      }
    }
  }
  // scores.splice(lowestHWID);
  if (lowestHWID >= 0)
    scores.splice(lowestHWID, 1);
  return scores;
};

var callback = function(err, r) {
  if (err) throw err;
  console.log('updated record');
  cursor.nextObject(updateScore);
};

var updateScore = function(err, doc) {
  if (err) throw err;

  if (doc === null)
    return dbSrc.close();
  var updatedScores = removeLowestHWScore(doc.scores);
  collection.update({_id:doc._id},
                       {$set: {scores: updatedScores }},
                       callback);
};

MongoClient.connect('mongodb://localhost:27017/school', function(err, db) {
  if(err) throw err;

  dbSrc = db;
  collection = db.collection('students');
  cursor = collection.find({});

  cursor.nextObject(updateScore);
});