forked from UO-CIS-322/proj6-mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_trial.py
60 lines (49 loc) · 1.46 KB
/
db_trial.py
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
"""
Just to test database functions,
outside of Flask.
We want to open our MongoDB database,
insert some memos, and read them back
"""
import arrow
# Mongo database
from pymongo import MongoClient
import CONFIG
try:
dbclient = MongoClient(CONFIG.MONGO_URL)
db = dbclient.memos
collection = db.dated
except:
print("Failure opening database. Is Mongo running? Correct password?")
sys.exit(1)
#
# Insertions: I commented these out after the first
# run successfuly inserted them
#
record = { "type": "dated_memo",
"date": arrow.utcnow().naive,
"text": "This is a sample memo"
}
collection.insert(record)
record = { "type": "dated_memo",
"date": arrow.utcnow().replace(days=+1).naive,
"text": "Sample one day later"
}
collection.insert(record)
#
# Read database --- May be useful to see what is in there,
# even after you have a working 'insert' operation in the flask app,
# but they aren't very readable. If you have more than a couple records,
# you'll want a loop for printing them in a nicer format.
#
records = [ ]
for record in collection.find( { "type": "dated_memo" } ):
records.append(
{ "type": record['type'],
"date": arrow.get(record['date']).to('local').isoformat(),
"text": record['text']
})
print(records)
#for record in collection.find( {"type": "dated_memo" } ):
# collection.remove(record)
print(records)
print("Tried to print records")