Computational Thinking + Doing
NoSQL Document Databases
Managing document-oriented data from the Nobel Prize—using Amazon DocumentDB, Azure Cosmos DB, and GCP Firestore.
Getting Started
If you are interested in reproducing this work, here are the versions of Python and Python packages used.
import sys
print(sys.version)
3.11.4 (v3.11.4:d2340ef257, Jun 6 2023, 19:15:51) [Clang 13.0.0 (clang-1300.0.29.30)]
!pip install requests==2.31.0
!pip install dnspython==2.4.1
import requests
import json
import dns
Implementing Locally/On-Premise With MongoDB/Couchbase/CouchDB on Docker
Since Amazon DocumentDB, Azure Cosmos DB, and GCP Firestore are paid cloud services, it will be prudent to model your tasks locally (on-premise, i.e., your personal computer), before attempting to replicate the same tasks on the cloud and possibly incurring costs while doing so.
MongoDB
A MongoDB NoSQL document store image on Docker will be used as the local server. The PyMongo package will be used as a Python interface to run queries on MongoDB.
!pip install pymongo==4.4.1
import pymongo
client = pymongo.MongoClient(localhost_host, localhost_port)
client.list_database_names()
['admin', 'config', 'local', 'nobel_db']
db = client[localhost_database]
print(db)
Database(MongoClient(host=['127.0.0.1:27017'], document_class=dict, tz_aware=False, connect=True), 'nobel_db')
collection = db["laureates"]
print(collection)
Collection(Database(MongoClient(host=['127.0.0.1:27017'], document_class=dict, tz_aware=False, connect=True), 'nobel_db'), 'laureates')
# To delete/reset database
collection.drop()
for collection_name in ["laureates"]:
response = requests.get("https://api.nobelprize.org/2.1/laureates")
documents = response.json()[collection_name]
collection.insert_many(documents)
<pymongo.results.InsertManyResult object at 0x118e138b0>
total_docs = collection.count_documents({})
total_docs
25
Couchbase
CouchDB
Implementing on the Cloud With Amazon DocumentDB
Implementing on the Cloud With Azure Cosmos DB
Implementing on the Cloud With GCP Firestore
Applied Computing