The Wayback Machine - https://web.archive.org/web/20240912192803/https://www.geeksforgeeks.org/connecting-mongodb-to-jupyter-notebook/
Open In App

Connecting MongoDB to Jupyter Notebook

Last Updated : 06 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Starting-MongoDB-Server

 

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.

Starting-MongoDB-Shell

 

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.

List-of-default-databases

 

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.

Installing-pymongo

 

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.

Fetching-collection-from-database

 

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?
> In this article, we will learn how to install Jupyter Notebook in Python on MacOS. The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. Uses include data cleaning and transformation, numerical simulation, statistical modeling, da
2 min read
How to install Jupyter Notebook in Linux?
Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. Uses include data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more. Jupyter has support for over 40 differ
2 min read
How to work with Julia on Jupyter Notebook?
Julia is a programming language that uses multiple dispatches as a feature where a method or a function can be dynamically dispatched based on the requirement. It is a high performance and a dynamic language which makes it user-interactive. It has a high-level syntax which makes it easy to learn and use for the beginner to expert level programmers.
3 min read
Jupyter Notebook Extension in Visual Studio Code
In this article, we will see how to use Jupyter Notebook in Visual Studio code. Jupyter Notebook has grown into a popular IDE choice. With the availability of additional kernels in addition to IPython, Jupyter Notebooks can also support Java, R, Julia and other popular programming languages ​​besides Python. The notebook integrates the outlines of
4 min read
Using R programming language in Jupyter Notebook
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. Jupyter has support for over 40 different programming languages and R Language is one of them. In this article, we will discuss how to use R Programming Language in Jupyter Noteb
1 min read
How to Install ipython-sql package in Jupyter Notebook?
ipython-sql is a %sql magic for python. This is a magic extension that allows you to immediately write SQL queries into code cells and read the results into pandas DataFrames. Using this, we can connect to any database which is supported SQLAlchemy. This is applicable to both classic notebooks and the current Jupyter Labs. In this article, we will
2 min read
Using Jupyter Notebook in Virtual Environment
In this article, we are going to see how to set Virtual Environment in Jupyter. Sometimes we want to use the Jupyter notebook in a virtual environment so that only selected packages are available in the scope of the notebook. To do this we have to add a new kernel for the virtual environment in the list of kernels available for the Jupyter notebook
2 min read
Resize the image in jupyter notebook using markdown
Markdown is a lightweight and popular Markup language. It is very similar to HTML (HyperText Markup Language). Markdown is mostly used by data analysts and data scientists to make their Jupyter Notebook more attractive, readable, and to give a website-like feeling to their notebook. Note: We can not use markdown directly into jupyter notebook becau
2 min read
Insert Image in a Jupyter Notebook
In this article, we will discuss how to insert images in a Jupyter Notebook. There are a few ways to insert images. These are - Direct insertion using the edit menu.Embedding in a markdown cellBy python code ( embedding an image in a code cell).Method 1: Direct insertion using the edit menu Step 1: This method is the easiest. first, change the type
2 min read
10 Interesting Jupyter Notebook Shortcuts and Extensions
Jupyter Notebook is equipped with numerous extensions and shortcuts which assist a lot of programmers, developers in their everyday operations. Such operations could be like doing data analysis of every single document comprising of computational explanatory text or adding easy links for common libraries like Pandas or Matplotlib. Moreover, all tho
9 min read
How to Install Scala in Jupyter IPython Notebook?
It is a very easy and simple process to Install Scala in Jupyter Ipython Notebook. You can follow the below steps to Install it. Before that, let us understand some related terms. The Jupyter Notebook is an open source web application that anyone can use to create documents as well as share the documents, which can contain live code, equations, and
2 min read
How to upload a dataset in Jupyter Notebook?
Jupyter Notebook is a web-based powerful IDE tool that helps with data analysis, visualization and narrative multi-media. Uploading a dataset is a very common task when working in the Jupyter Notebook. It is widely used by data analysts, data scientists, researchers and other developers to perform data analysis, machine learning tasks and other sci
4 min read
How to run an .ipynb Jupyter Notebook from terminal?
Let's see how to run Notebook in Jupyter from a terminal in this article, but first, let's get a sense of what these terms mean. Python: Python is a versatile and high-level programming language known for its readability and extensive libraries, making it a popular choice for web development, data analysis, artificial intelligence, and more.Jupyter
2 min read
How to Optimize Jupyter Notebook Performance ?
IntroductionJupiter Notebook is one of the best (if not the best) online applications/environments which is used heavily to do tasks related to Data Analysis, Data Science, Machine Learning, Deep Learning, etc. in Python. Jupyter Notebook doesn't need to be installed locally, it can be run entirely in online mode. The sections in which the code is
10 min read
Keyboard Shortcuts to Clear Cell Output in Jupyter Notebook
In this article, we will cover How to clear the output of a cell in Jupyter Notebook we will discuss various methods to do the same. First, we look at what is Jupyter Notebook and why we use it, we discuss different methods to clear cell output in Jupyter Notebook in this article. Jupyter NotebookThe Jupyter Notebook is the original web application
3 min read
How to Hide all Codes in Jupyter Notebook
Jupyter Notebooks have become indispensable tools for data scientists, researchers, and educators alike. They provide an interactive and dynamic environment for writing code, documenting processes, and presenting results. However, when it comes to sharing your work, particularly in lectures or presentations, the raw code in Jupyter Notebooks can so
4 min read
Ways to recover deleted Jupyter notebook cell?
Jupyter Notebook stands as an important tool for its interactive and dynamic environment in the arena of data science and artificial intelligence. These Notebooks are a versatile tool that can be used for a variety of purposes and are a great tool for documenting and sharing your work, learning new skills, and training machine/deep learning models
4 min read
Interactive Graphs in Jupyter Notebook
When working in a Jupyter Notebook environment, you can produce interactive Matplotlib plots that allow you to explore data and interact with the charts dynamically. In this article, we'll explore how to create such interactive plots using Matplotlib within Jupyter. Before we proceed with the steps, let's understand some key concepts related to pro
3 min read
Magic Commands for Profiling in Jupyter Notebook
Jupyter Notebook is a versatile tool or IDE widely used mostly by data scientists, researchers, and programmers for interactive computing and data analysis, dashboards, and visualizations. It offers a unique and rich set of features. Some topics are there it can perform such as: running the code in different languages, timing the code execution, de
9 min read
How to Delete Markdown in Jupyter Notebook
In this article, we will cover How to delete markdown in Jupyter Notebook we will discuss various methods to do the same. First, we look at what is Jupyter Notebook and markdown and why we use them, we discuss different methods to delete a markdown in Jupyter Notebook in this article. Jupyter NotebookThe Jupyter Notebook is the original web applica
4 min read
Data Visualization in jupyter notebook
In this article, we will learn how to visualize data in Jupyter Notebook there are different libraries available in Python for data visualization like Matplotlib, seaborn, Plotly, GGPlot, Bokeh, etc. But in this article, we will use different libraries like Matplotlib, searborn, and Plotly which are widely used for data visualization. We will gener
7 min read
How to Write and Run Code in Jupyter Notebook
Jupyter Notebook is an open-source web application. It allows to generate and share documents that contain live code, equations, visualized data, and many more features. Nowadays it has become the first choice of many of the data scientists due to it's immense data visualization and analysis capability. It has various packages which makes it easy t
7 min read
How to Work in Jupyter Notebook with Multiple Languages
Jupyter Notebook is a popular IDE (Integrated Development Environment) mostly used in Data fields. Jupyter Notebook is a very versatile platform. Here we can work with multiple programming languages with the help of changing kernels, we just need to use different kernels for different languages. Each kernel is responsible for interpreting and execu
10 min read
How to Customize Line Graph in Jupyter Notebook
In this article, we will learn how to customize a line graph in Jupyter Notebook, we will discover different customization options available to customize the line graph we will use matplotlib library to generate and customize line graphs we will understand each option by looking it into the syntax along with the examples, Matplotlib is a comprehens
6 min read
How to Add a Table of Contents in the Jupyter Notebook
In this article, we will cover How to add a table of contents to the Jupyter Notebook. First, we look at what Jupyter Notebook and table of contents and why we use them, followed by a step-by-step tutorial to add a table of contents to the Jupyter Notebook. Jupyter NotebookThe Jupyter Notebook is the original web application for creating and sharin
4 min read
Jupyter Notebook
Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. It is a popular tool among data scientists, researchers, and educators for interactive computing and data analysis. The name "Jupyter" is derived from the three core programming lang
5 min read
Jupyter Notebook - Cell Magic Functions
In this article, we will cover Cell Magic Functions in Jupyter Notebook we will discuss various functions. But first, we look at what Jupyter Notebook and Cell Magic functions and why we use them. There are a lot of cell magic functions but in this article, we discuss the most commonly used cell magic functions. Jupyter NotebookThe Jupyter Notebook
5 min read
How to Fix Kernel Error in Jupyter Notebook
Jupyter Notebook is an open-source framework that comes under Anaconda software. It is a free cloud-based interactive platform used for computing in different types of languages. It is used for data analysis, visualizations, numerical calculations and simulations, equations, executing codes, machine learning, and sharing data easily. So, while perf
15 min read
Configure Keyboard Shortcuts in Jupyter Notebook
Jupyter Notebook is the original web application for creating and sharing computational documents that contain live code, equations, visualizations, and narrative text. It offers a simple, streamlined, document-centric experience. Jupyter has support for over 40 different programming languages and Python is one of them. In this article, we will cov
1 min read
Understanding Jupyter Notebook Widgets
Jupyter Notebook Widgets help us create an interactive user interface in a Jupyter Notebook. Jupyter Widgets are interactive browser controls for Jupyter notebooks. Jupyter Notebook Widgets are collective items that can be added to Jupyter Notebooks to improve user knowledge by giving users the ability to handle and visualize data dynamically. In t
6 min read