-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhw3-1.js
63 lines (54 loc) · 1.55 KB
/
hw3-1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var MongoClient = require('mongodb').MongoClient
MongoClient.connect('mongodb://127.0.0.1:27017/school', function (err, db) {
if (err) throw err
var cursor = db.collection('students').find()
// var sort = []
// var operator = { '$inc': { 'counter': 1 } }
// var options = { 'new': 1 }
cursor.each(function (err, doc) {
if ( doc ) {
// console.log(doc.scores.homework)
var scores = doc.scores.map(function (score) {
if (score.type == 'homework') {
return score.score
}
}).filter(function (val) {
return val != null
})
// console.log(scores)
var smaller = Math.min.apply(null, scores)
// console.log('smaller:' + smaller)
var lowerIndex
doc.scores.forEach(function (score, i) {
// console.log(score, i)
if (score.type == 'homework') {
if (score.score == smaller) {
lowerIndex = i
}
}
})
// console.log(lowerIndex, smaller)
var update = doc.scores.splice(lowerIndex, 1)
// console.log(doc.scores)
var operator = {
'$set': {
'scores': doc.scores
}
}
db.collection('students').update({ _id: doc._id }, operator, function (err, student) {
if (err) throw err
if (student) {
console.log('Updated ' + student + ' student')
// console.log('Found student with _id:' + doc._id + ':' + student._id)
// console.log(student._id + ':' + student.name + ': now try to update his scores!')
}
// else {
// console.log('Could not find student with _id ' + doc._id)
// }
if (student._id == 199) {
return db.close()
}
})
}
})
})