Connecting MongoDB to Jupyter Notebook
Last Updated :
06 Jun, 2022
MongoDB is one of the most famous NoSql databases. It is an open-source database. The simple meaning of the NoSql database is a non-relational database. i.e., It doesn't include the specific structure of databases such as tables and all like the relational database. So, you can design the schemas without any restrictions.
Jupyter Notebook is also an open-source web application that allows users to create and share documents that contain live code, equations, visualizations, and narrative text. By using the Jupyter notebook, programmers can make the code clean and debug and execute it step by step. We can use the 40+ different languages with the Jupyter notebook, and among the most popular is Python.
In this tutorial, we will connect the MongoDb to the Jupyter Notebook in the python language.
Prerequisites:
Now, follow the below steps to start the MongoDB server on localhost and connect it to the Jupyter Notebooks.
Step 1: After installing the MongoDB on your Windows, start the MongoDB server by entering the below command to the command prompt.
mongod
You will see the following logs on your terminal. Also, at the end of the records, you can see that server has been started on the 27017 port.
Step 2: Now, open the other command prompt window and write the below command to start the mongo shell. Ensure that you haven't closed the first command prompt window and the server is still running without any error.
mongo
You will see the following logs on your terminal.
Step 3: You can enter the below commands to see your default databases.
show dbs
On the terminal, you will see the following outputs. Maybe the names of your databases can be different.
Step 4: Launch Jupyter NoteBook and create a new Jupyter Notebook file.
Step 5: We will use the python PyMongo module to connect the Jupyter Notebook with the MongoDB localhost. So, write the below code into the NoteBook cell.
!pip install pymongo
Note: Users can run the Jupyter NoteBook cell by pressing the Shift + Enter.
Step 6: In the next cell, import the PyMongo module and execute the cell. If the cell runs without any error, PyMongo module is successfully installed.
import pymongo
from pymongo import MongoClient
Step 7: Next, connect the MongoDB database using the below line of code. Don't forget to execute the cell of the Jupyter Notebooks.
client = MongoClient("localhost", 27017)
Step 8: Now, add the below code to the next cell and execute it to fetch any collection from the database. Here, we are fetching the 'test' collection.
db = client['test']
db
You will see the following logs on your NoteBook file.
You have connected the MongoDB database with the Jupyter NoteBook. Now, you can perform the same operation from Jupyter NoteBook that you can perform from the Mongo shell.
Similar Reads
How to Install Jupyter Notebook on MacOS Jupyter Notebook is a popular web-based interactive computing environment, widely used among data scientists and programmers. Working with Jupyter Notebook in MacOS helps perform various tasks including data cleaning and transformation, numerical simulation, statistical modelling, data visualization
5 min read
How to Install Jupyter Notebook in Linux Jupyter Notebook is a powerful, open-source tool for interactive computing, widely used for data analysis, machine learning, and scientific research. If you're using Linux and want to install Jupyter Notebook, then this guide is for you. Here, we're going to discuss seamless way to download and inst
3 min read
How to Connect Node to a MongoDB Database ? Connecting Node.js to MongoDB is a common task for backend developers working with NoSQL databases. MongoDB is a powerful, flexible, and scalable database that stores data in a JSON-like format. In this step-by-step guide, we'll walk through the entire process from setting up your development enviro
6 min read
Connect Django Project to MongoDB Djongo is a SQL to MongoDB query transpiler. Using djongo, we can use MongoDB as a backend database for our Django project. We don't even need to change the Django ORM. The best part is that we can setup Django with MongoDB by adding just one line of code. There is no need to change serializers, vie
2 min read
How to use MongoDB Connection String MongoDB connection strings are essential for establishing connections between applications and MongoDB databases. These strings contain crucial information such as server addresses, authentication credentials and optional parameters, enabling seamless communication. Understanding the structure and c
6 min read