-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoDB.py~
executable file
·57 lines (43 loc) · 1.51 KB
/
mongoDB.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
#!/usr/bin/python
import pymongo
import sys
from bson import ObjectId
class mongoDB:
def __init__(self, db):
client = pymongo.MongoClient('127.0.0.1', 27017)
self.db = client[db]
def insert(self, collection, document):
collection = self.db[collection]
collection.insert(document)
def update(self, collection, post):
collection = self.db[collection]
OID = post["_id"]
post.pop("_id")
collection.update({"_id":OID},{"$set":post})
def find_KV(self, collection, key, val):
posts = self.db[str(collection)].find({key : val})
ret = []
for post in posts:
ret.append(post)
return ret
def find_LT(self, collection, key, val):
posts = self.db[str(collection)].find({str(key) : {'$lte' : val}})
ret = []
for post in posts:
ret.append(post)
return ret
def find_GT(self, collection, key, val):
posts = self.db[str(collection)].find({str(key) : {'$gte' : val}})
ret = []
for post in posts:
ret.append(post)
return ret
def find_scope(self, collection, key, low, high):
posts = self.db[str(collection)].find({str(key) : {'$lte' : high, "$gte" : low}})
ret = []
for post in posts:
ret.append(post)
return ret
def find(self, tp, dic):
ret = []
return ret