-
Notifications
You must be signed in to change notification settings - Fork 1
/
database_operations.py
65 lines (57 loc) · 1.72 KB
/
database_operations.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
from pymongo import MongoClient
def get_collections():
client = MongoClient()
paper = client['paper_db']
paper_coll = paper['paper_info']
org_coll = paper['organization_collection']
country_coll = paper['country_code']
return paper_coll, org_coll, country_coll
def get_papers():
papers, _, _ = get_collections()
result = []
for paper in papers.find({}):
result.append({
'title': paper['title'],
'authors': paper['authors'],
'abstract': paper['abstract'],
'organization': paper['organization'],
'url': paper['link']
})
return result
def get_countries():
papers, _, _ = get_collections()
pipeline = [{
'$lookup': {
'from': 'organization_collection',
'localField': 'organization',
'foreignField': 'name',
'as': 'country'
}
}]
paper_result = []
for r in papers.aggregate(pipeline):
paper_result.append({
'title': r['title'],
'organization': r['organization'],
'country': r['country'][0]['country_code']
})
return paper_result
def get_distribute():
numbers = {}
for paper in get_countries():
code = paper['country']
if code in numbers:
numbers[code] += 1
else:
numbers[code] = 1
#print(numbers, sum(numbers.values()))
result = []
_, _, countries = get_collections()
for country in countries.find({}):
num = numbers[country['code']] if country['code'] in numbers else 0
result.append({
'id': country['code'],
'name': country['name'],
'value': num
})
return result