{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Connecting to MongoDB\n", "\n", "We'll start with some examples to test our connection to MongoDB and how to send commands to the database." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Select the Python Kernel\n", "\n", "To run the code cells in these notebooks you need to select the appropriate kernel (programming language). In this case is Python. Click on the `Select Kernel` button at the top right, then `Python Environments` > `Choose the recommended Python environment`. For this workshop, we will use Python 3.11." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install the MongoDB driver\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pymongo import MongoClient" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set your connection string below\n", "\n", "We can just connect to `localhost` as this devcontainer is also running a MongoDB instance with some data, or you can connect to your own Atlas cluster just changing the `connectionString` below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mongoDBURI = \"mongodb://admin:mongodb@localhost:27017/?directConnection=true\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Connect to MongoDB and ping the Database server\n", "\n", "Connect and ping() database command fetches information about the state of the MongoDB database." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "client = MongoClient(mongoDBURI)\n", "\n", "try:\n", " response = client.admin.command({\"ping\": 1})\n", " print(\"=> Result of the {'ping': 1} command:\\n\", response)\n", " print(\"Connected to MongoDB!\")\n", "except Exception as e:\n", " print(\"Error connecting to MongoDB:\", e )\n", "\n", "#Database name\n", "db = client[\"library\"]\n", "\n", "books = db[\"books\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Send a Hello command\n", "\n", "The `hello()` command returns database server details for the MongoDB database." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "client = MongoClient(mongoDBURI)\n", "\n", "try:\n", " response = client.admin.command({\"hello\": 1})\n", " print(\"=> Result of the {'hello': 1} command:\\n\", response)\n", "except Exception as e:\n", " print(\"Error running command:\", e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get a list of collections in the database" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Get the list of databases\n", "db_names = client.list_database_names()\n", "print(\"Database names:\", db_names)\n", "\n", "# Get the list of collections in the 'library' database\n", "collections = db.list_collections()\n", "\n", "for c in collections:\n", " print(f'Collection: {c[\"name\"]}')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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" } }, "nbformat": 4, "nbformat_minor": 2 }