Python MongoDB
MongoDB is one of the most popular NoSQL databases, using BSON (similar to JSON) data types.
For MongoDB database installation and introduction, please refer to our MongoDB Tutorial.
PyMongo
To connect Python to MongoDB, you need a MongoDB driver. Here, we use the PyMongo driver to establish the connection.
pip Installation
pip is a general-purpose Python package management tool, providing functionalities for searching, downloading, installing, and uninstalling Python packages.
Install pymongo:
$ python3 -m pip3 install pymongo
You can also specify the version to install:
$ python3 -m pip3 install pymongo==3.5.1
Update pymongo command:
$ python3 -m pip3 install --upgrade pymongo
easy_install Installation
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
Testing PyMongo
Next, we can create a test file named demo_test_mongodb.py
with the following code:
demo_test_mongodb.py File Code:
#!/usr/bin/python3
import pymongo
Executing the above code file, if no errors occur, indicates successful installation.
Creating a Database
Creating a Database
Creating a database requires a MongoClient object and specifying the connection URL and the database name.
In the following example, we create a database named tutorialprodb
:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
Note: In MongoDB, a database is only created after content is inserted! This means that after creating the database, you need to create a collection (table) and insert a document (record) for the database to be actually created.
Checking if a Database Exists
We can retrieve all databases in MongoDB and check if the specified database exists:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
dblist = myclient.list_database_names()
# dblist = myclient.database_names()
if "tutorialprodb" in dblist:
print("The database already exists!")
Note: database_names
is deprecated in the latest versions of Python. For Python 3.7+ and later, it has been replaced with list_database_names()
.
Creating a Collection
Collections in MongoDB are similar to tables in SQL.
Creating a Collection
MongoDB uses a database object to create a collection, as shown in the following example:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["sites"]
Note: In MongoDB, a collection is only created after content is inserted! This means that after creating the collection (table), you need to insert a document (record) for the collection to be actually created.
Checking if a Collection Exists
We can retrieve all collections in a MongoDB database and check if the specified collection exists:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['tutorialprodb']
collist = mydb.list_collection_names()
# collist = mydb.collection_names()
if "sites" in collist: # Check if the "sites" collection exists
print("Collection already exists!")
Note: collection_names is deprecated in the latest versions of Python, replaced by list_collection_names() in Python 3.7+ versions.
Insert, Delete, Update, Query Operations
The table below lists more operations for MongoDB, click on the specific links for details:
No. | Function |
---|---|
1 | Insert Data |
2 | Query Data |
3 | Update Data |
4 | Sort Data |
5 | Delete Data |