-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmongo.py
91 lines (57 loc) · 2.56 KB
/
mongo.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from pymongo import MongoClient
import all_comps
def init_database():
global client
client = MongoClient("mongodb://db:27017")
global db
db = client['scrader']
db.authenticate('scrader', 'scr@d3r')
companies_stored = db['companies']
if companies_stored.count() == 0:
insert_many('companies', all_comps.all_companies)
return db
def insert_one(collection_name, document):
collection = db[collection_name]
return collection.insert_one(document)
def delete_many(collection_name):
collection = db[collection_name]
return collection.delete_many({})
def count(collection_name):
return db[collection_name].count()
def fetch_collection(collection_name):
collection = db[collection_name]
return collection.find({}, {'_id': False})
def find_matches(collection_name, to_match):
collection = db[collection_name]
return collection.find(to_match)
def find_matches_containing_many(collection_name, field, match):
collection = db[collection_name]
return collection.find({field: {'$in': match}})
def find_matches_two_fields(collection_name, field1, match1, field2, match2):
collection = db[collection_name]
return collection.find({'$and': [{field1: {'$in': match1}}, {field2: {'$in': match2}}]})
def find_matches_three_fields(collection_name, field1, match1, field2, match2, field3, match3):
collection = db[collection_name]
return collection.find({'$and': [{field1: {'$in': match1}}, {field2: {'$in': match2}},
{field3: {'$in': match3}}]})
def find_matches_containing_list(collection_name, field, value):
collection = db[collection_name]
return collection.find({field: {'$in': value}})
def find_matches_not_containing(collection_name, field, value):
collection = db[collection_name]
return collection.find({field: {'$nin': value}})
def find_one_match(collection_name, to_match):
collection = db[collection_name]
return collection.find_one(to_match)
def insert_one_in(collection_name, to_match, new_data):
collection = db[collection_name]
return collection.update(to_match, {'$set': new_data})
def remove_one_from(collection_name, to_match, data_to_remove):
collection = db[collection_name]
return collection.update(to_match, {'$unset': data_to_remove})
def delete_one_from(collection_name, item_id_to_remove):
collection = db[collection_name]
return collection.remove({'_id': item_id_to_remove})
def insert_many(collection_name, to_add):
collection = db[collection_name]
return collection.insert_many(to_add)