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.
PyMongo
Section titled “PyMongo”To connect Python to MongoDB, you need a MongoDB driver. Here we use the PyMongo driver.
pip Installation
Section titled “pip Installation”pip is a general-purpose Python package management tool that provides functions for finding, downloading, installing, and uninstalling Python packages.
Install pymongo:
You can also specify a version to install:
Update pymongo command:
easy_install Installation
Section titled “easy_install Installation”Older versions of Python can use easy_install for installation. easy_install is also a Python package management tool.
Update pymongo command:
Testing PyMongo
Section titled “Testing PyMongo”Next, we can create a test file demo_test_mongodb.py with the following code:
demo_test_mongodb.py File Code:
Section titled “demo_test_mongodb.py File Code:”If no errors occur when executing the above code file, it means the installation was successful.
Creating a Database
Section titled “Creating a Database”Creating a Database
Section titled “Creating a Database”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:
Example
Section titled “Example”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.
Checking if a Database Already Exists
Section titled “Checking if a Database Already Exists”We can read all databases in MongoDB and check if a specified database exists:
Example
Section titled “Example”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().
Creating a Collection
Section titled “Creating a Collection”Collections in MongoDB are similar to tables in SQL.
Creating a Collection
Section titled “Creating a Collection”MongoDB uses database objects to create collections, as shown in the following example:
Example
Section titled “Example”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.
Checking if a Collection Already Exists
Section titled “Checking if a Collection Already Exists”We can read all collections in the MongoDB database and check if a specified collection exists:
Example
Section titled “Example”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 |