Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
materialsproject
GitHub Repository: materialsproject/workshop
Path: blob/master/binder/populate_nobel_database.py
2488 views
1
import requests
2
from pymongo import MongoClient
3
4
# Client connects to "localhost" by default
5
client = MongoClient()
6
# Create local "nobel" database on the fly
7
db = client["nobel"]
8
9
# API documented at https://nobelprize.readme.io/docs/prize
10
for collection_name in ["prizes", "laureates"]:
11
singular = collection_name[:-1]
12
response = requests.get("http://api.nobelprize.org/v1/{}.json".format(singular))
13
documents = response.json()[collection_name]
14
# Access collections on the fly!
15
db[collection_name].drop() # Drop collection first if already exists
16
db[collection_name].insert_many(documents)
17
18
# Check to make sure everything is correct
19
assert client.nobel == client["nobel"]
20
assert client.nobel.prizes == client["nobel"]["prizes"]
21
print(db.prizes.count_documents({}), "prize documents")
22
print(db.laureates.count_documents({}), "laureates documents")
23
24