Created
November 17, 2012 16:27
-
-
Save mrmarcondes/4097305 to your computer and use it in GitHub Desktop.
10gen: M101 MongoDB for Developers - Homework 3.1
This file contains 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
require 'mongo' | |
#ruby | |
connection = Mongo::Connection.new | |
db = connection.db("school") | |
students = db.collection("students").find().sort({student_id:1}) | |
student_id = -1 | |
while students.has_next? do | |
student = students.next | |
if student_id != student["_id"] | |
higher_homework = Float(0.0) | |
new_scores = [] | |
student["scores"].each do |score| | |
if score["type"].eql?("homework") | |
current_score = score["score"] | |
if current_score > higher_homework | |
higher_homework = current_score | |
end | |
else | |
new_scores.push score | |
end | |
end | |
new_scores.push({"type"=> "homework", "score"=> higher_homework}) | |
student_id = student["_id"] | |
student["scores"] = new_scores | |
db.collection("students").update({"_id" => student["_id"]}, student) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment