Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var MongoClient = require('mongodb').MongoClient
- MongoClient.connect('mongodb://localhost:27017/school', function (err, db) {
- panicIf(err)
- var collection = db.collection('students')
- var students = collection.find({})
- students.each(function (err, student) {
- panicIf(err)
- if (student) {
- var max = (student.scores[2].score > student.scores[3].score)? 2 : 3
- var newscores = [student.scores[0], student.scores[1], student.scores[max]]
- student.scores = newscores
- var query = {"_id" : student._id}
- collection.update(query, student, function(err, doc) {
- panicIf(err)
- console.log('Updated')
- console.dir(doc)
- console.log('\n\n')
- })
- } else {
- db.close()
- }
- })
- })
- function panicIf(err) {
- if (err) {
- throw err
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement