Python Mongodb
# Python3.x Python MongoDB
MongoDB is currently one of the most popular NoSQL databases, using the BSON data type (similar to JSON).
You can check our (#) for MongoDB database installation and introduction.
* * *
## PyMongo
Python needs a MongoDB driver to connect to MongoDB. Here we use the PyMongo driver to connect.
### pip Installation
pip is a universal 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 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 to 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` with the following code:
## demo_test_mongodb.py File Code:
#!/usr/bin/python3 import pymongo
Execute the above code file. If no errors appear, the installation was successful.
* * *
## Creating a Database
### Creating a Database
To create a database, you need to use the `MongoClient` object and specify the connection URL and the database name to create.
In the following example, we create the database `tutorialdb`:
## Example
#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclient
> **Note:** In MongoDB, a database is only created after content is inserted! This means that after creating a database, you must create a collection (table) and insert a document (record) for the database to be truly created.
### Checking if a Database Exists
We can read all databases in MongoDB and check if a specific database exists:
## Example
#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient('mongodb://localhost:27017/')dblist = myclient.list_database_names()# dblist = myclient.database_names() if"tutorialdb"in dblist: print("Database already exists!")
> **Note:** `database_names` is deprecated in the latest versions of Python. For Python 3.7+, it has been changed to `list_database_names()`.
* * *
## Creating a Collection
A collection in MongoDB is similar to a table in SQL.
### Creating a Collection
MongoDB uses the database object to create a collection, as shown in the example below:
## Example
#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/")mydb = myclientmycol = mydb
> **Note:** In MongoDB, a collection is only created after content is inserted! This means that after creating a collection (table), you must insert a document (record) for the collection to be truly created.
### Checking if a Collection Exists
We can read all collections in a MongoDB database and check if a specific collection exists:
## Example
#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient('mongodb://localhost:27017/')mydb = myclient['tutorialdb']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 P
YouTip