简体   繁体   中英

node.js and mongodb find and delete

hey guys im new to programming in general and i am trying to write a query in node.js to display the lowest score in my database then later on delete it. my data looks like this

{
"_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
    }
]
}

so i need to find and delete just the lowest homework alone and not both. in the code i wrote i am trying to just display these lowest first then i will remove them. so far this code i have written shows both scores like this

'Verdell Sowinski scored 69.09840625499065'

'Verdell Sowinski scored 17.90304994248164'

'Vina Matsunaga scored 19.16579262350994'

'Vina Matsunaga scored 8.217818112853726'

'Vinnie Auerbach scored 23.91300715707971'

'Vinnie Auerbach scored 53.31631231243156'

'Whitley Fears scored 92.2308421188758'

'Whitley Fears scored 97.95928979563497'

'Wilburn Spiess scored 10.53058536508186'

'Wilburn Spiess scored 28.10477578379966'

'Zachary Langlais scored 19.21886443577987'

'Zachary Langlais scored 8.548735651522431'

'aimee Zank scored 6.676176060654615'

'aimee Zank scored 18.52035674134503'

this is what i have so far :

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

thanks in advance

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

   });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM