-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathusing_update.py
More file actions
72 lines (56 loc) · 1.82 KB
/
using_update.py
File metadata and controls
72 lines (56 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pymongo
import sys
import datetime
connection = pymongo.MongoClient("mongodb://localhost")
def remove_review_date():
db = connection.school
scores = db.scores
print "removing records with review date"
try:
scores.update({},{'$unset':{'review_date':1}}, multi=True)
except:
print "Unexpected error:", sys.exc_info()[0]
def using_save():
db = connection.school
scores = db.scores
print "updating records using save"
try:
score = scores.find_one({'student_id':1,'type':'homework'})
print "before ", score
score['review_date'] = datetime.datetime.utcnow()
scores.save(score)
score = scores.find_one({'student_id':1,'type':'homework'})
print "after ", score
except:
print "Unexpected error:", sys.exc_info()[0]
def using_update():
db = connection.school
scores = db.scores
print "updating records using update"
try:
score = scores.find_one({'student_id':1,'type':'homework'})
print "before ", score
score['review_date'] = datetime.datetime.utcnow()
scores.update({'student_id':1,'type':'homework'},score)
score = scores.find_one({'student_id':1,'type':'homework'})
print "after ", score
except:
print "Unexpected error:", sys.exc_info()[0]
def using_set():
db = connection.school
scores = db.scores
print "updating records using set"
try:
score = scores.find_one({'student_id':1,'type':'homework'})
print "before ", score
scores.update({'student_id':1,'type':'homework'}, {'$set':{'review_date':datetime.datetime.utcnow()}})
score = scores.find_one({'student_id':1,'type':'homework'})
print "after ", score
except:
print "Unexpected error:", sys.exc_info()[0]
remove_review_date()
using_save()
remove_review_date()
using_update()
remove_review_date()
using_set()