-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmongodb_query.py
62 lines (42 loc) · 1.22 KB
/
mongodb_query.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
from pymongo import MongoClient
import pymongo
import inspect
import os
import util as UTIL
env_vars = UTIL.load_json_config(os.environ['OPENMTURK_CONFIG'])[1]
client = MongoClient()
db = client[env_vars['OPENMTURK_TABLE_NAME']]
def select_all(query_dict={}):
return list(db.labels_db.find(query_dict))
def select_attr(query_dict, filter_dict):
return list(db.labels_db.find(query_dict, filter_dict))
def insert_label(data):
db.labels_db.update_one(
{'img_path': data['img_path']},
{'$set': data},
upsert=True
)
def delete_label(data):
result = db.labels_db.update_one(
{'img_path': data['img_path']},
{'$set':{'is_labelled': False,
'bbs': [],
'category': '',
'orientation': ''}}
)
def select_label(img_path):
label = []
log_prefix = 'Client request - {}'.format(inspect.stack()[0][3])
try:
label = db.labels_db.find({'img_path': img_path})[0]
del label['_id'] # not json-friendly object
print('{} - labels for image {}: existing labels found'.format(
log_prefix,
img_path))
except Exception as e:
print('{} - labels for image {}: None found.'.format(
log_prefix,
img_path))
return label
def count_labels():
return int(db.labels_db.count({'is_labelled': True}))