Skip to content

Python MongoDB

MongoDB is one of the most popular NoSQL databases, using the BSON data type (similar to JSON).

For MongoDB database installation and introduction, check out our MongoDB tutorial.


To connect Python to MongoDB, you need a MongoDB driver. Here we use the PyMongo driver.

pip is a general-purpose Python package management tool that provides functions for finding, downloading, installing, and uninstalling Python packages.

Install pymongo:

$ python3 -m pip3 install pymongo

You can also specify a version to install:

$ python3 -m pip3 install pymongo==3.5.1

Update pymongo command:

$ python3 -m pip3 install --upgrade pymongo

Older versions of Python can use easy_install for installation. easy_install is also a Python package management tool.

$ python -m easy_install pymongo

Update pymongo command:

$ python -m easy_install -U pymongo

Next, we can create a test file demo_test_mongodb.py with the following code:

#!/usr/bin/python3
 
import pymongo

If no errors occur when executing the above code file, it means the installation was successful.


To create a database, you need to use the MongoClient object and specify the connection URL address and the database name to be created.

In the following example, we create the database runoobdb:

#!/usr/bin/python3
 
import pymongo
 
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["runoobdb"]

Note: In MongoDB, a database is only created after content is inserted! That is, after creating a database, you must create a collection (data table) and insert a document (record) for the database to actually be created.

We can read all databases in MongoDB and check if a specified database exists:

#!/usr/bin/python3
 
import pymongo
 
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
 
dblist = myclient.list_database_names()
# dblist = myclient.database_names() 
if "runoobdb" in dblist:
  print("Database already exists!")

Note: database_names has been deprecated in the latest versions of Python. In Python 3.7+ and later, it has been renamed to list_database_names().


Collections in MongoDB are similar to tables in SQL.

MongoDB uses database objects to create collections, as shown in the following example:

#!/usr/bin/python3
 
import pymongo
 
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["runoobdb"]
 
mycol = mydb["sites"]

Note: In MongoDB, a collection is only created after content is inserted! That is, after creating a collection (data table), you must insert a document (record) for the collection to actually be created.

We can read all collections in the MongoDB database and check if a specified collection exists:

#!/usr/bin/python3
 
import pymongo
 
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
 
mydb = myclient['runoobdb']
 
collist = mydb. list_collection_names()
# collist = mydb.collection_names()
if "sites" in collist:   # Check if sites collection exists
  print("Collection already exists!")

Note: collection_names has been deprecated in the latest versions of Python. In Python 3.7+ and later, it has been renamed to list_collection_names().


Insert, Delete, Update, and Query Operations

Section titled “Insert, Delete, Update, and Query Operations”

The table below lists more MongoDB operations. Click the specific links for details:

No. Function
1 Insert Data
2 Query Data
3 Update Data
4 Sort Data
5 Delete Data