Skip to content

Instantly share code, notes, and snippets.

@sunnydoll
Created October 29, 2015 01:55
Show Gist options
  • Save sunnydoll/098049c3d7f5a74dcf24 to your computer and use it in GitHub Desktop.
Save sunnydoll/098049c3d7f5a74dcf24 to your computer and use it in GitHub Desktop.
MongoDB query for deleting the lowest score from scores array with type of homework
{
"_id" : 137,
"name" : "Tamika Schildgen",
"scores" : [
{
"type" : "exam",
"score" : 4.433956226109692
},
{
"type" : "quiz",
"score" : 65.50313785402548
},
{
"type" : "homework",
"score" : 89.5950384993947
},
{
"type" : "homework",
"score" : 80.9348234982713
}
]
}
res=db.students.aggregate([{"$unwind":"$scores"},{"$match":{"scores.type":"homework"}},{"$group":{'_id':'$_id','minscore':{'$min':"$scores.score"}}}])
res.forEach(function(coll){db.students.update({'_id':coll._id}, {'$pull':{'scores':{'score':coll.minscore}}})})
db.students.aggregate( { '$unwind' : '$scores' } , { '$group' : { '_id' : '$_id' , 'average' : { $avg : '$scores.score' } } } , { '$sort' : { 'average' : -1 } } , { '$limit' : 1 } )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment