DEV Community 👩‍💻👨‍💻

Suttipong Kullawattana
Suttipong Kullawattana

Posted on • Updated on

How to setup MongoDB with Jupyter Notebook

I have to conclusion how to setup MongoDB (NoSQL) with Jupyter Notebook by summary step like this.

First step, Install python 3.9.1 for use on python, install $ pip install pymongo.

Second step, Install homebrew with $ /bin/bash -c "$(curl -fsSL"

Third step, Install Jupyter Notebook on Windows, MacOS or Linux and try to install $ pip install notebook in terminal and run $ jupyter notebook

Fourth step, Install MongoDB Community Edition on macOS mongoDB and MongoDB Compass

install $ brew tap mongodb/brew and $ brew install mongodb-community

Image description

Install MongoDB on Ubuntu

$ sudo apt update

$ sudo apt install wget curl gnupg2 software-properties-common apt-transport-https ca-certificates lsb-release

$ curl -fsSL|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-6.gpg

$ echo "deb [ arch=amd64,arm64 ] focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

$ wget

$ sudo dpkg -i ./libssl1.1_1.1.1f-1ubuntu2.16_amd64.deb

$ sudo apt update

$ sudo apt install mongodb-org

$ sudo systemctl enable --now mongod

Checking config of mongod.conf on development mode

$ sudo vi /etc/mongod.conf

#add this line
          authorization: enabled
# network interfaces
      port: 27017
      bindIp:  # Change to global
Enter fullscreen mode Exit fullscreen mode

Restart service again with $ sudo service mongod restart

Create the “db” directory where MongoDB stores its database by $ mkdir -p /data/db

Make sure that the directory has the previlage to read and write from the directory by using chown (change owner) with $ chown -R 'id -un'/data/db

Start MongoDB command : $ 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.

On Ubuntu, You can followup setup directory like this.
$ sudo systemctl stop mongod.service
$ sudo mkdir -p /newdata/mongo
$ sudo chown -R mongodb:mongodb /newdata/mongo
$ sudo rsync -av /var/lib/mongodb /newdata/mongo
$ sudo mv /var/lib/mongodb /var/lib/mongodb.bak
$ sudo ln -s /newdata/mongo /var/lib/mongodb
$ sudo systemctl daemon-reload
$ sudo systemctl start mongod

Five step, Install mongosh for start mongosh, MongoDB Shell and create role user on database

$ mongosh
    test> use admin
    test> db.createUser({user: "myUserAdmin", pwd: "abc123",  roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]})
    test> exit
$ mongosh -u mongdbuser -p --authenticationDatabase admin
Enter password: ********
Enter fullscreen mode Exit fullscreen mode

Image description

write the code below

import pymongo
from pymongo import MongoClient

client = MongoClient('mongodb://%s:%s@' % (username, password))

my_collection = db["patient_data"]

patient_record = {
   "Name": "Maureen Skinner",
   "Age": 87,
   "Sex": "F",
   "Blood pressure": [{"sys": 156}, {"dia": 82}],
   "Heart rate": 82


for item in my_collection.find():
Enter fullscreen mode Exit fullscreen mode

Result on MongoDB Compass

Image description

Stop MongoDB command : $ brew services stop mongodb-community

Reference: MongoDB tutorial, mongosh, Building Data Pipeline | Spotify - Jupyter Notebook - MongoDB, Connecting MongoDB to Jupyter Notebook, Example Query NoSQL, Setting user account, Using MongoDB in python, Install Jupyter Notebook on Ubuntu

Top comments (0)

Classic DEV post:

CLI tools