-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
28 lines (23 loc) · 781 Bytes
/
database.py
File metadata and controls
28 lines (23 loc) · 781 Bytes
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
from pymongo import MongoClient
from bson.objectid import ObjectId
import datetime
client = MongoClient('mongodb://localhost:27017/')
db = client['journal_db']
entries_collection = db['entries']
def create_entry(title, content):
entry = {
"title": title,
"content": content,
"date": datetime.datetime.now()
}
result = entries_collection.insert_one(entry)
return result.inserted_id
def get_entries():
return list(entries_collection.find().sort("date", -1))
def get_entry_by_id(entry_id):
return entries_collection.find_one({"_id": ObjectId(entry_id)})
def update_entry(entry_id, title, content):
entries_collection.update_one(
{"_id": ObjectId(entry_id)},
{"$set": {"title": title, "content": content}}
)