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 https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

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 https://www.mongodb.org/static/pgp/server-6.0.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-6.gpg

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

$ wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2.16_amd64.deb

$ 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
    security:
          authorization: enabled
# network interfaces
    net:
      port: 27017
      bindIp: 0.0.0.0  # 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

username='myUserAdmin'
password='abc123'
client = MongoClient('mongodb://%s:%s@127.0.0.1' % (username, password))
db=client['mongotestdb']

my_collection = db["patient_data"]

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

my_collection.insert_one(patient_record)

for item in my_collection.find():
    print(item)
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