Working with MongoDB and Python
Tags: python, mongodb, beginners, tutorial
Published: 2020-06-04 10:46:51

MongoDB is a NoSQL, general purpose, document-based, distributed database. It stores data in a familiar JSON like format which many developers are comfortable working with. In this article, let’s take a look on how to get started working with MongoDB in a Python environment by performing basic Create, Read, Update and Delete (CRUD) operations.

<h4>Why MongoDB?</h4> 
Traditional Relational Database Systems (RDBMS) have a steep learning curve as one has to get comfortable with database concepts and SQL. Since MongoDB has no strict schema requirements, it is relatively very easy to get started. Here is a <a href="https://www.mongodb.com/scale/nosql-vs-relational-databases">comparison</a> between NoSQL and RDBMS.
 
<h4>Basic MongoDB terms</h4>
In order to use MongoDB, one needs to understand basic terms used by MongoDB. MongoDB arranges data by document. So, a row of data in MongoDB is called a **document**. In RDBMS, tables contain number of rows while in MongoDB tables are called **collections** as they contain a group of documents.  
Below table lists basic relational database terms along with its equivalent in MongoDB: 

| RDBMS Concept | MongoDB Equivalent |
| ------------- | ------------------ | 
| Database      | Database           | 
| Tables        | Collections        |
| Rows          | Documents          |
| Columns       | Fields             |
| Index         | Index              |

<h4>How to install MongoDB?</h4>
Installing MongoDB using docker is straight forward. Here are the commands to install using docker.
``` python
docker pull mongo
#Running Mongo as a background task with data mapped to external home/mongo/data folder 
docker run -d mongo -v /home/mongo/data:/data/db --name mongodb
```   

This <a href="https://docs.mongodb.com/manual/administration/install-community/">link</a> contains installation instructions for other environments.

<h4>How to work with MongoDB in Python?</h4>
To work with MongoDB in Python, one needs to install `PyMongo` driver using command - `python -m pip install pymongo`

<h4>How to Create a Collection in MongoDB?</h4>
Below file connects to MongoDB installed in the local environment and creates a database called `booksdb` containing `books` collection.
```python
from pymongo import MongoClient

#Step 1: Connect to MongoDB - Change connection string as needed
client = MongoClient(port=27017)
db=client.booksdb

#Step 2: Prepare sample data
book_list = ["Automate the Boring Stuff with Python: Practical Programming for Total Beginners",
          	"Python for Everybody: Exploring Data in Python 3",
          	"Dive Into Python 3",
          	"Test Book"]
author_list = ["Al Sweigart",
           	"Dr. Charles Russell Severance, Sue Blumenberg, Elliott Hauser, Aimee Andrion",
            	"Mark Pilgrim",
            	"Test Author"]
publication_year_list = [2015, 2016, 2012, 2020]

#Step 3: Insert sample data
for idx in range(0, 4):
	book = {
    	'title' : book_list[idx],
    	'author' : author_list[idx],
    	'pub_year' : publication_year_list[idx]
	}
	#Step 4: Insert book object by using MongoDB’s isnert_one
	result=db.books.insert_one(book)
	#Step 5: Print ObjectID of the new document
	print(f'Created {idx+1} of 4 as {result.inserted_id}')

print('Finished creating books')
```
Please note MongoDB automatically adds a `_id` field with unique id value to every document during document creation.
 
<h4>How to query Collections in MongoDB?</h4>
Below listing shows how to query a collection matching a criteria and query all documents in a collection.
```python
from pymongo import MongoClient
from pprint import pprint

#Connect to MongoDB
client = MongoClient(port=27017)
db=client.booksdb

# Print one of the books matching criteria
book = db.books.find_one( { 'pub_year': 2020} )
pprint(book)

# Print all of the books
books = db.books.find( { } )
for bk in books:
	pprint(bk)
```

<h4>How to update Collections in MongoDB?</h4>
Unlike traditional databases, new fields could be added to a particular row of data. Below listing adds the `copies_sold` column to a book `document` with publication year of 2015. 
```python
from pymongo import MongoClient
from pprint import pprint

#Connect to MongoDB
client = MongoClient(port=27017)
db=client.booksdb

#Find document
book = db.books.find_one( { 'pub_year': 2015} )
pprint(book)

#Update document
result = db.books.update_one({'_id' : book.get('_id') }, {'$inc': {'copies_sold': 50}})
print('Number of documents modified : ' + str(result.modified_count))

#Print updated document
updated_book = db.books.find_one({'_id':book.get('_id')})
print('The updated document:')
pprint(updated_book)
```
<h4>How to delete documents in MongoDB?</h4>
Here is a sample listing containing document deletion matching a certain criteria. 
```python
from pymongo import MongoClient

#Connect to MongoDB
client = MongoClient(port=27017)
db=client.booksdb

#Delete document matching sample criteria
result = db.books.delete_many({"title": "Test Book"})

#Print deletion count
print(f'Deleted {result.deleted_count} documents!')
```

<h4>Further Reading</h4>
To know more about MongoDB please visit <a href="https://docs.mongodb.com/manual/introduction/">MongoDB documentation.