Skip to content

Instantly share code, notes, and snippets.

@hperantunes
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hperantunes/d31023911884a852f8c3 to your computer and use it in GitHub Desktop.
Save hperantunes/d31023911884a852f8c3 to your computer and use it in GitHub Desktop.
M101P - Homework 2.2
db.grades.distinct('student_id').forEach(function(id) {
db.grades.findAndModify({
query: { student_id: id, type: 'homework' },
sort: { score: 1 },
remove: true
});
})
// 600
db.grades.count()
// { "_id" : ObjectId("50906d7fa3c412bb040eb709"), "student_id" : 100, "type" : "homework", "score" : 88.50425479139126 }
db.grades.find().sort({'score':-1}).skip(100).limit(1)
db.grades.find({},{'student_id':1, 'type':1, 'score':1, '_id':0}).sort({'student_id':1, 'score':1, }).limit(5)
// query:
db.grades.aggregate({'$group':{'_id':'$student_id', 'average':{$avg:'$score'}}}, {'$sort':{'average':-1}}, {'$limit':1})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment