Skip to content

Instantly share code, notes, and snippets.

@bashilbers
Created January 29, 2014 10:14
Show Gist options
  • Save bashilbers/8685084 to your computer and use it in GitHub Desktop.
Save bashilbers/8685084 to your computer and use it in GitHub Desktop.
M101JS 3.1
var mongo = require('mongoskin');
var _ = require('underscore')._;
var db = mongo.db('mongodb://localhost:27017/school');
var collection = db.collection('students');
var cursor = collection.find();
cursor.each(function(err, doc) {
if (!doc) return;
console.log("\n" + '-------START of document-------' + "\n");
console.log("doc:", doc);
var homework = _.sortBy(_.filter(doc.scores, function(score) {
return score.type === 'homework'
}), 'score');
var lowestScore = homework[0];
console.log("homework scores:", homework);
console.log('lowest homework score: ', homework[0]);
if (lowestScore) {
collection.update({ _id: doc._id}, { '$pull': { scores: { type: 'homework', score: lowestScore.score }}});
}
console.log("\n" + '-------END of document-------' + "\n");
});
console.log('starting....');
@bashilbers
Copy link
Author

Even if this is not the best way, it works :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment