Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var MongoClient = require('mongodb').MongoClient
  2.  
  3. MongoClient.connect('mongodb://localhost:27017/school', function (err, db) {
  4.     panicIf(err)
  5.  
  6.     var collection = db.collection('students')
  7.  
  8.     var students = collection.find({})
  9.     students.each(function (err, student) {
  10.         panicIf(err)
  11.         if (student) {
  12.             var max = (student.scores[2].score > student.scores[3].score)? 2 : 3
  13.             var newscores = [student.scores[0], student.scores[1], student.scores[max]]
  14.             student.scores = newscores
  15.  
  16.             var query = {"_id" : student._id}
  17.  
  18.             collection.update(query, student, function(err, doc) {
  19.                 panicIf(err)
  20.                 console.log('Updated')
  21.                 console.dir(doc)
  22.                 console.log('\n\n')
  23.             })
  24.  
  25.         } else {
  26.             db.close()
  27.         }
  28.     })
  29. })
  30.  
  31. function panicIf(err) {
  32.     if (err) {
  33.         throw err
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement