node.js和mongodb查找和删除

时间:2015-11-03 09:56:58

标签: node.js mongodb nosql

嘿,伙计们我一般都是编程的新手,我试图在node.js中编写一个查询来显示我数据库中的最低分,然后再删除它。我的数据看起来像这样

{
"_id" : 19,
"name" : "Gisela Levin",
"scores" : [
    {
        "type" : "exam",
        "score" : 44.51211101958831
    },
    {
        "type" : "quiz",
        "score" : 0.6578497966368002
    },
    {
        "type" : "homework",
        "score" : 93.36341655949683
    },
    {
        "type" : "homework",
        "score" : 49.43132782777443
    }
]
}

所以我需要找到并删除单独的最低功课,而不是两者。在我写的代码中我试图只显示这些最低的然后我将删除它们。到目前为止,我写的这个代码显示了这样的分数

  

' Verdell Sowinski得分为69.09840625499065'

     

' Verdell Sowinski得分17.90304994248164'

     

' Vina Matsunaga得分19.16579262350994'

     

' Vina Matsunaga得分8.217818112853726'

     

' Vinnie Auerbach得分23.91300715707971'

     

' Vinnie Auerbach得分53.31631231243156'

     

'惠特利恐惧得分92.2308421188758'

     

'惠特利恐惧得分97.95928979563497'

     

' Wilburn Spiess得分10.53058536508186'

     

' Wilburn Spiess得分28.10477578379966'

     

' Zachary Langlais得分19.21886443577987'

     

' Zachary Langlais得分8.548735651522431'

     

' aimee Zank得分6.676176060654615'

     

' aimee Zank得分为18.5203​​5674134503'

这是我到目前为止所做的:

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

    var cursor = db.collection('students').find().sort({name : 1});
    var list = [];
    var count = 0;
    var currentName = '';

    cursor.each(function(err, doc){
        if(err) throw err;

    if(doc == null){
        return
    }

    var lowestScore = 1000;
    var lowestScore_position = '';

    for(i=0; i<doc.scores.length; i++){
        if (doc.scores[i].type == 'homework' && doc.scores[i].score<=lowestScore) {
        lowestScore = doc.scores[i].score;
        lowestScore_position = i;

        }
    }
    console.dir(doc.name + " scored " + lowestScore);
    });
});

提前致谢

1 个答案:

答案 0 :(得分:1)

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

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

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

   var lastStudent = '';

   cursor.each(function(err, doc){
      if (err) throw err;

      if (doc == null) {
          setTimeout(function() {
              return db.close()
          }, 2000);
      } else {

          var lowScoreIndex = -1;
          var lowScore = 100;

          Array.prototype.forEach.call(doc.scores, function(score) {
              if (score.type === 'homework') {
                  if (score.score < lowScore) {
                      lowScore = score.score;
                      lowScoreIndex = Array.prototype.indexOf.call(score, lowScore);
                  }
              }
          });

          doc.scores.splice(lowScoreIndex, 1);

          db.collection('students').update({'_id':doc['_id']}, doc, function(err, updated) {
              if (err) throw err;
          })
      }

   });
});