Created
October 29, 2015 01:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_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