{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Reading Data from MongoDB\n", "\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/AccelerationConsortium/ac-microcourses/blob/main/docs/courses/hello-world/1.5.1-pymongo.ipynb)\n", "\n", "Within [the data logging notebook](./1.5-data-logging.ipynb), 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](https://pymongo.readthedocs.io/en/stable/). 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." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# only install if we are running in colab\n", "import sys\n", "IN_COLAB = 'google.colab' in sys.modules\n", "if IN_COLAB:\n", " %pip install pymongo pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, instantiate the PyMongo client and connect to the database." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pinged your deployment. You successfully connected to MongoDB!\n" ] } ], "source": [ "from pymongo.mongo_client import MongoClient\n", "\n", "# normally, the MongoDB credentials would be kept private\n", "# but for the purposes of this tutorial we will share them\n", "MONGODB_PASSWORD = \"HGzZNsQ3vBLKrXXF\"\n", "\n", "# Connection string obtained via MongoDB Atlas \"Connect\" button\n", "blinded_connection_string = \"mongodb+srv://test-user-find-only:@test-cluster.c5jgpni.mongodb.net/?retryWrites=true&w=majority\"\n", "\n", "# Replace with the MongoDB password (where again, the connection\n", "# string and password would normally be kept private)\n", "connection_string = blinded_connection_string.replace(\"\", MONGODB_PASSWORD)\n", "\n", "# Create a new client and connect to the server\n", "client = MongoClient(connection_string)\n", "\n", "# Send a ping to confirm a successful connection\n", "try:\n", " client.admin.command('ping')\n", " print(\"Pinged your deployment. You successfully connected to MongoDB!\")\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'_id': ObjectId('65962c0458f0b76b37484b82'), 'course_id': 'happy-panda'}, {'_id': ObjectId('65962c3758f0b76b37484b83'), 'course_id': 'amused-zebra'}, {'_id': ObjectId('65962c4958f0b76b37484b84'), 'course_id': 'sorrowful-hippo'}]\n" ] } ], "source": [ "database_name = \"test-db\"\n", "collection_name = \"read-from-me\"\n", "\n", "db = client[database_name]\n", "collection = db[collection_name]\n", "\n", "# get all results\n", "results = list(collection.find({}))\n", "\n", "print(results)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a pandas DataFrame from the collection entries." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
course_id
_id
65962c0458f0b76b37484b82happy-panda
65962c3758f0b76b37484b83amused-zebra
65962c4958f0b76b37484b84sorrowful-hippo
\n", "
" ], "text/plain": [ " course_id\n", "_id \n", "65962c0458f0b76b37484b82 happy-panda\n", "65962c3758f0b76b37484b83 amused-zebra\n", "65962c4958f0b76b37484b84 sorrowful-hippo" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# NOTE: your course ID will not appear in the results\n", "import pandas as pd\n", "df = pd.DataFrame(results).set_index(\"_id\")\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, export the DataFrame to a CSV file and print the CSV file contents." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "_id,course_id\n", "65962c0458f0b76b37484b82,happy-panda\n", "65962c3758f0b76b37484b83,amused-zebra\n", "65962c4958f0b76b37484b84,sorrowful-hippo\n", "\n" ] } ], "source": [ "df.to_csv(\"results.csv\")\n", "\n", "with open('results.csv', 'r') as file:\n", " print(file.read())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Close the connection to MongoDB." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "client.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Additional Resources\n", "\n", "- [PyMongo Project-based Tutorial using FastAPI](https://www.mongodb.com/languages/python/pymongo-tutorial)\n", "- [PyMongo Docs Tutorial](https://pymongo.readthedocs.io/en/stable/tutorial.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once you've successfully run this notebook, return to the [data logging notebook](./1.5-data-logging.ipynb)." ] } ], "metadata": { "kernelspec": { "display_name": "ac-microcourses", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 2 }