Skip to content

Instantly share code, notes, and snippets.

@pvoznenko
Last active August 29, 2015 14:05
Show Gist options
  • Save pvoznenko/3e7a879cac8c2511c7ca to your computer and use it in GitHub Desktop.
Save pvoznenko/3e7a879cac8c2511c7ca to your computer and use it in GitHub Desktop.
hw3.1
var cursor = db.students.aggregate([{$unwind: "$scores"}, {$match: {"scores.type": "homework"}}, {$sort: {"scores.score": 1}}]);
var ids = []; while (cursor.hasNext()) { var data = cursor.next(); if (ids.indexOf(data._id) === -1) { ids.push(data._id); db.students.update(data, {$pull: {"scores": data.scores}}); } }
// or
db.students.aggregate([{"$unwind":"$scores"}, {$match: {"scores.type": "homework"}}, {"$sort": {"scores.score": 1}}, {$group: {_id: "$_id", lowScore: {$first: "$scores.score"}, scores: {$push: "$scores"}}}]).forEach(function(myDoc){db.students.update({"_id": myDoc._id}, {$pull: {scores: {score: myDoc.lowScore}}})});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment