Python MongoDB
Python MongoDB
MongoDB is one of the most popular NoSQL databases.
For more information on MongoDB database installation and introduction, please refer to our MongoDB Tutorial.
PyMongo
Python Connecting to MongoDB requires a MongoDB driver. Here, we use the PyMongo driver.
Pip Installation
Pip is a general-purpose Python package management tool that provides functionality 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
Installation with easy_install
Older versions of Python can be installed using easy_install, which 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, demo_test_mongodb.py. The code is as follows:
demo_test_mongodb.py file code:
#!/usr/bin/python3
import pymongo
Execute the above code file. If no errors occur, the installation is successful.
Creating a Database
Creating a Database
Creating a database requires using a MongoClient object and specifying the connection URL and the name of the database to be created.
In the following example, we create the database geekdocdb:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["geekdocdb"]
Note: In MongoDB, a database is not created until content is inserted! This means that after the database is created, a collection (table) must be created and a document (record) inserted before the database is actually created.
Determine if a database exists
We can read all databases in MongoDB and determine whether a 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 "geekdocdb" in dblist:
print("Database exists!")
Note: database_names has been deprecated in recent versions of Python. Versions starting with Python 3.7 use list_database_names() instead.
Creating a Collection
A collection in MongoDB is similar to a table in SQL.
Creating a Collection
MongoDB uses database objects to create collections. The following example shows this:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["geekdocdb"]
mycol = mydb["sites"]
Note: In MongoDB, a collection is created only after content is inserted! This means that after creating a collection (table), insert a document (record) before the collection is actually created.
Determine if a collection exists
We can read all collections in a MongoDB database and determine if a specified collection exists:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['geekdocdb']
collist = mydb.list_collection_names()
# collist = mydb.collection_names()
if "sites" in collist: # Check if the "sites" collection exists
print("Collection exists!")
Note: collection_names is deprecated in the latest versions of Python. Versions starting with Python 3.7 or later use list_collection_names().
Add, delete, modify, and query operations
The following table lists MongoDB For more operations, please click on the specific link for details:
Number | Function |
---|---|
1 | Add Data |
2 | Query Data |
3 | Modify Data |
4 | Sort Data |
5 | Delete Data |