Example #1
0
from dbConnection.db import DB
import pymongo

mongo = DB("mongodb://*****:*****@ds037395.mongolab.com:37395/mongodbcourse", "mongodbcourse", "students")

# dump all
mongo.get_collection().delete_many({})
print "deleted all the documents in the collection , current count : " + str(mongo.get_collection().count())

# create new
mongo.import_collections("grades.json")
print "successfully import new documents , the current count : " + str(mongo.get_collection().count())

# The assignment is
# Write a program in the language of your choice that will remove the grade of type "homework" \
# with the lowest score for each student from the dataset in the handout.
# Since each document is one grade, it should remove one document per student.
# This will use the same data set as the last problem, but if you don't have it, you can download and re-import.
# My query is the following :
queries = []
queries.append({"$match": {"type": "homework"}})
queries.append({"$group": {"_id": "$student_id", "score": {"$min": "$score"}, "type": {"$addToSet": "$type"}}})


result = mongo.get_collection().aggregate(queries)

for doc in result:
    res = mongo.get_collection().delete_one({"student_id": doc["_id"], "type": doc["type"][0], "score": doc["score"]})
print "after deleting there are " + str(mongo.get_collection().count()) + " documents"

# looking for db.grades.find().sort( { 'score' : -1 } ).skip( 100 ).limit( 1 )