Reading Data from MongoDB

Open In Colab

Within the data logging notebook, you learned how to write data to a MongoDB database directly from a microcontroller using MongoDB’s data API. In this notebook, you will learn how to read data from a MongoDB database using the official MongoDB Python driver: PyMongo. Note that you will read data from a separate database collection than the one you wrote to in the previous notebook. While it would be more instructive to read the same data that you wrote, to avoid potential misuse, users only have write access to the database collection from the previous notebook. Likewise, in this notebook, users only have read access to a separate database collection with a fixed set of data.

# only install if we are running in colab
import sys
IN_COLAB = 'google.colab' in sys.modules
if IN_COLAB:
    %pip install pymongo pandas

First, instantiate the PyMongo client and connect to the database.

from pymongo.mongo_client import MongoClient

# normally, the MongoDB credentials would be kept private
# but for the purposes of this tutorial we will share them
MONGODB_PASSWORD = "HGzZNsQ3vBLKrXXF"

# Connection string obtained via MongoDB Atlas "Connect" button
blinded_connection_string = "mongodb+srv://test-user-find-only:<password>@test-cluster.c5jgpni.mongodb.net/?retryWrites=true&w=majority"

# Replace <password> with the MongoDB password (where again, the connection
# string and password would normally be kept private)
connection_string = blinded_connection_string.replace("<password>", MONGODB_PASSWORD)

# Create a new client and connect to the server
client = MongoClient(connection_string)

# Send a ping to confirm a successful connection
try:
    client.admin.command('ping')
    print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
    print(e)
Pinged your deployment. You successfully connected to MongoDB!

Next, read all entries from the collection. In this case, there are only three pre-existing entries in the collection (uploaded by the course developers). This will not include your course ID for reasons listed above.

database_name = "test-db"
collection_name = "read-from-me"

db = client[database_name]
collection = db[collection_name]

# get all results
results = list(collection.find({}))

print(results)
[{'_id': ObjectId('65962c0458f0b76b37484b82'), 'course_id': 'happy-panda'}, {'_id': ObjectId('65962c3758f0b76b37484b83'), 'course_id': 'amused-zebra'}, {'_id': ObjectId('65962c4958f0b76b37484b84'), 'course_id': 'sorrowful-hippo'}]

Create a pandas DataFrame from the collection entries.

# NOTE: your course ID will not appear in the results
import pandas as pd
df = pd.DataFrame(results).set_index("_id")
df
course_id
_id
65962c0458f0b76b37484b82 happy-panda
65962c3758f0b76b37484b83 amused-zebra
65962c4958f0b76b37484b84 sorrowful-hippo

Finally, export the DataFrame to a CSV file and print the CSV file contents.

df.to_csv("results.csv")

with open('results.csv', 'r') as file:
    print(file.read())
_id,course_id
65962c0458f0b76b37484b82,happy-panda
65962c3758f0b76b37484b83,amused-zebra
65962c4958f0b76b37484b84,sorrowful-hippo

Close the connection to MongoDB.

client.close()

Additional Resources

Once you’ve successfully run this notebook, return to the data logging notebook.