-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb_utils.py
37 lines (29 loc) · 982 Bytes
/
mongodb_utils.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
from pymongo import MongoClient
# Define MongoDB connection parameters
MongoDB_HOST = 'localhost'
MongoDB_DB_NAME = 'academicworld'
# Function to fetch data from the MongoDB localhost
def mongodb_aggregate(collection, pipeline):
"""
Attempts to return MongoDB aggregate query and return list of dicts corresponding to documents
Parameters:
collection (str) : Name of collection to query
pipeline (list of dicts): List of MongodB operations
Returns:
list of dicts: Document list
Example:
#>>> MongoDB_aggregate("publications", [{"$match": {"year": {"$gte": 2012}}}, {"$project":
#{ "_id": 0, "keywords.name": 1}}])
Returns:
{'keywords': [
{'name': 'wavelet'},
{'name': 'convex program'},
...
]},
"""
client = MongoClient(MongoDB_HOST, 27017) # 22943
db = client[MongoDB_DB_NAME]
col = db[collection]
result = list(col.aggregate(pipeline))
client.close()
return result