Advertisement
vyoumans

node.js update of mongoDB problem...

Sep 1st, 2013
5,207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. I am doing a node.js and mongo assignment...
  2. The test is to loop through a mongoDB find the lowest grade scores, and remove them. I was able to write something that would find the lowest grades, remove them from a secondary var with splice()
  3. but now I want to update the original document with the new array... and this is the part I am having difficulty with.
  4.  
  5. the first code is really just the PRETTY() version of some mondodb records that I am querrying... the SCORES array is the important part.
  6. The second program will create a doc and doc.scores with the corrected data. But I am trying to update the original DOC with the new data. Not sure how to do this.
  7.  
  8. ~~~
  9. > db.students.find({_id:113}).pretty();
  10. {
  11. "_id" : 113,
  12. "name" : "",
  13. "scores" : [
  14. {
  15. "type" : "exam",
  16. "score" : 77.57315913088024
  17. },
  18. {
  19. "type" : "quiz",
  20. "score" : 13.28135073340091
  21. },
  22. {
  23. "type" : "homework",
  24. "score" : 67.27527802263116
  25. },
  26. {
  27. "type" : "homework",
  28. "score" : 55.74198976046431
  29. }
  30. ]
  31. }
  32.  
  33. ~~~
  34.  
  35. my program would splice out doc.scores[1] which has the lowest score.
  36.  
  37. Now I want to update the original db. My code is below...
  38.  
  39.  
  40. ~~~
  41.  
  42. var MongoClient = require('mongodb').MongoClient;
  43. MongoClient.connect('mongodb://localhost:27017/school',
  44. function(err, db) {
  45.  
  46. if(err) throw(err);
  47.  
  48. var cur = db.collection('students').find({}).sort({name:1});
  49. var updates = 0;
  50. var curDone=false;
  51. cur.each(function(err, doc){
  52. if(err) throw err;
  53. if (doc != null){
  54. console.dir(doc._id + "-- record "+ doc.name);
  55.  
  56. zz = doc.scores;
  57. zzlowScore = zz[0].score;
  58. zzlowScoreIndex = 0;
  59. for(var i = 0, l = zz.length; i < l; i++ ){
  60. console.dir("========zz--Index "+i+" --- score: "+ zz[i].score);
  61. if(zzlowScore > zz[i].score){
  62. //we have a new low score and index
  63. zzlowScore = zz[i].score;
  64. zzlowScoreIndex= i;
  65. };
  66. };
  67.  
  68. console.dir("=== low score Index "+zzlowScoreIndex + " ==lowscore== "+zzlowScore);
  69.  
  70. console.dir("..............remove the bad stuff..."+ doc._id);
  71. doc.scores.splice(zzlowScoreIndex,1);
  72.  
  73. zz = doc.scores;
  74. for(var i = 0, l = zz.length; i < l; i++ ){
  75. console.dir("====zz--Index "+i+" --- score: "+ zz[i].score);
  76. };
  77. console.dir("============================================================================");
  78. //doc.update({_id:doc._id},{scores:zz});
  79. //db.collection('students').update({_id:doc._id},{scores:zz});
  80. //
  81. //Now I need to pass the new doc.scores back to the DB so it can update scores subarray.
  82. //But not sure how to do this.
  83.  
  84.  
  85.  
  86. //cur.update({_id:doc._id},$set:{scores:zz});
  87. db.schools.update({_id:doc._id},$set:{scores:zz});
  88. //!!!!!!!!!!!!!! THIS IS THE PART I CAN NOT FIGURE OUT. HOW TO UPDATE ORIGINAL DOC??????
  89.  
  90. //return doc.scores;
  91.  
  92. }else{
  93. db.close();
  94. };
  95.  
  96.  
  97. console.dir("++++++++++++++++++++++++++++++++++++++++++");
  98.  
  99.  
  100. });
  101. });
  102.  
  103.  
  104. ~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement