Connecting MongoDB to Jupyter notebook
MongoDB is a NoSQL database. This database is able to handle huge volumes of data very efficiently. MongoDB does not have restriction on schema design. Unlike traditional relation database, where data is stored in clearly defined tables and columns, and each column contains a very specific type of data. MongoDB has a dynamic schemas for unstructured data. Data can be stored as a (i) KeyValue, (ii) document-oriented, (iii)column-oriented, or (iv) graph-based.
In this article, I will show how to install MongoDB on macOS.
MongoDB on mac can be downloaded through MongoDB wesite or homebrew website. Here, we are using homebrew to download. First we need to download homebrew. To download homebrew click on homebrew website. On the webiste you will see a link. Copy the link.
Open terminal and paste the code in the terminal. Enter your password. This process will install homebrew package. Once the homebrew package is installed we will install MongoDB. You will see “Installation successful!” message on the bottom of the page if the installation is successful. Now we are ready to install MongoDB.
Install MongoDB
To install MongoDB, we will use the following command:
brew install mongodb-community
After installing, we need to create the “db” directory where MongoDB stores its database. You can create the directory by running:
mkdir -p /data/db
We have to make sure that the directory has the previlage to read and write from the directory by using chown (change owner):
chown -R 'id -un'/data/db
Now we have a db directory and the process can read from the directory. We are ready to start the MongoDB using :
brew services start mongodb-community
Using ps aux | grep -v grep |grep mongod,
allow us to verify that MongoDB is running. You should see the current ststus of your mongod process.
To begin using MongoDB, type mongo to the terminal.
This shows our MongoDB is up and running.
Now, we need to install MongoDB python driver to access the MongoDB. Open a jupyter notebook and type !pip install pymongo
. Now we are connected to MongoDB. If import pymongo
is executed with no errors, pymongo module is installed.
To create the database in MongoDB, we need to create MongoClient.
First we will import pymongo. Pymongo library allows interaction with the MongoDB database through Python. Next we need to create a MongoClient object. MongoClient represents a client connection to one or more MongoDB servers. We will use function from pymongo driver called MongoClient to create a MongoClient object. We also have to specify a host ( here we are using localhost) and port (27017). Once we have a connected instance of MongoClient, we can access database within the Mongo server. To access database we want to use we have to specify the database name. If the database doesn’t exist. It will automatically creates the database.
We can stop MongoDB using : brew services stop mongodb-community
In this article, we showed you how to connect your MongoDB to a jupyter notebook.