-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
43 lines (32 loc) · 936 Bytes
/
database.py
File metadata and controls
43 lines (32 loc) · 936 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
29
30
31
32
33
34
35
36
37
38
39
40
41
#
#
from pymongo import MongoClient
DB_URL = 'mongodb://{dbuser}:{dbpassword}@ds135790.mlab.com:35790/maindb'
class db():
def __init__(self, user, password):
self.client = MongoClient(DB_URL.format(dbuser=user, dbpassword=password))
self.db = self.client['maindb']
self.db.authenticate(user, password)
print self.db
self.collection = self.db.posts
print "[DB] CONNECTION SUCCESSFULL"
def insert_document(self, post):
self.collection.insert_one(post)
print "[DB] DOCUMENT INSERTED"
def get_document(self, key):
return self.collection.find_one(key)
def find_documents(self, key=None):
return self.collection.find(key)
def update_post(self, key, field_key, field_value):
self.collection.update_one(
key,
{
'$set': {
field_key:field_value
}
}
)
print "[DB] DOCUMENT UPDATED"
def delete_documents(self, key):
self.collection.delete_many(key)
print "[DB] DOCUMENTS DELETED"