示例#1
0
文件: 2_2.py 项目: olsynt/mongoCourse
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 )
示例#2
0
    errors['verify_error'] = ""
    errors['email_error'] = ""

    if not USER_RE.match(username):
        errors['username_error'] = "invalid username. try just letters and numbers"
        return False

    if not PASS_RE.match(password):
        errors['password_error'] = "invalid password."
        return False
    if password != verify:
        errors['verify_error'] = "password must match"
        return False
    if email != "":
        if not EMAIL_RE.match(email):
            errors['email_error'] = "invalid email address"
            return False
    return True


connection = DB("mongodb://*****:*****@ds037395.mongolab.com:37395/mongodbcourse", "mongodbcourse", "users")
collection = connection.get_collection()

users = userDAO.UserDAO(connection.get_data_base())
sessions = sessionDAO.SessionDAO(connection.get_data_base())


bottle.debug(True)
bottle.run(host='localhost', port=8082)         # Start the webserver running and wait for requests

示例#3
0
文件: 2_1.py 项目: olsynt/mongoCourse
from dbConnection.db import DB
mongo = DB("mongodb://*****:*****@ds037395.mongolab.com:37395/mongodbcourse", "mongodbcourse", "students")

queries = []
queries.append({'$match': {'score': {'$gte': 65}, 'type': 'exam'}})
queries.append({'$group': {'_id': '$student_id', 'score': {'$min': '$score'}, 'type': {'$addToSet': '$type'}}})
queries.append({'$sort': {'score': 1}})
queries.append({'$limit': 1})

# the result will be 22
for a in mongo.get_collection().aggregate(queries):
    print a