-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_mongo.py
81 lines (64 loc) · 1.8 KB
/
database_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
import pymongo
from pymongo import MongoClient
import json
from bson.json_util import dumps
client = pymongo.MongoClient(
"mongodb+srv://tanishq:[email protected]/rcsb?retryWrites=true&w=majority")
db = client['rcsb']
collection = db['rcsb']
def insert_data(data):
try:
collection.insert_one(data)
return True
except Exception as e:
print(e)
return False
def json_get():
with open("temp.json", "r") as inp:
read_data = inp.read()
filedata = json.loads(read_data)
for data in filedata:
status = check_key_duplicate(data['key'])
if status == False:
if(insert_data(data)):
print("Successfully Inserted the data!")
else:
print("Insertion failed!")
else:
print("Duplicate Data")
# get all the data
def query():
data_ob = collection.find()
return data_ob
# get the data count
def get_document_count(filters):
query = {}
if filters:
query.update(filters)
collection_count = collection.count_documents(query)
return collection_count
def check_key_duplicate(key):
query = {"key": {"$eq": key}}
count = get_document_count(query)
if count == 0:
return False
return True
# the searching part
def search_chains(info):
info = ".*"+info+".*"
temp = collection.find({'chain': {'$regex': info}}, {'_id': 0})
# temp = collection.find({'chain': {'$regex': info}}, {'_id': 0, 'chain': 0})
ans = []
for i, t in enumerate(temp):
# for only url
# dic = {}
# dic['no'] = i
# dic['url'] = t['url']
# ans.append(dic)
ans.append(t)
return ans
if __name__ == "__main__":
json_get()
info = query()
for i in info:
print(type(i))