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, see our MongoDB Tutorial.
PyMongo
To connect to MongoDB in Python, you need a MongoDB driver; here we use the PyMongo driver to connect.
pip installation
pip is a general-purpose Python package management tool that provides functions to find, download, install, and uninstall 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
Command to update pymongo:
$ python3 -m pip3 install --upgrade pymongo
easy_install installation
Older versions of Python can use easy_install to install; easy_install is also a Python package management tool.
$ python -m easy_install pymongo
Command to update pymongo:
$ python -m easy_install -U pymongo
Test PyMongo
Next we can create a test file demo_test_mongodb.py with the following code:
Code for demo_test_mongodb.py:
#!/usr/bin/python3
import
pymongo
Run the above code file; if no error occurs, the installation is successful.
Create the database
Create a database
Creating a database requires a MongoClient object, and you must specify the connection URL and the name of the database to create.
In the example below, we create the database runoobdb:
Example
#!/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 the database you must create a collection (table) and insert a document (record) for the database to actually be created.
Check whether a database already exists
We can read all databases in MongoDB and check whether 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
"
runoobdb
"
in
dblist
:
(
"
The database already exists!
"
)
Note:database_names has been deprecated in the latest version of Python; versions after Python 3.7+ changed it to list_database_names().
Create a collection
A collection in MongoDB is similar to a table in SQL.
Create a collection
MongoDB uses the database object to create a collection, as shown in the example:
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 the collection (table) you must insert a document (record) for the collection to actually be created.
Check whether a collection already exists
We can read all collections in the MongoDB database and check whether the specified collection exists:
Example
#!/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 whether the sites collection exists
(
"
The collection already exists!
"
)
Note:collection_names has been deprecated in the latest version of Python; versions after Python 3.7+ changed it to list_collection_names().
Operations such as create, delete, update, and query
The table below lists more MongoDB operations; click the specific link for details:
| No. | Function |
|---|---|
| 1 | Add data |
| 2 | Query data |
| 3 | Update data |
| 4 | Data sorting |
| 5 | Delete data |