Python MongoDB Tutorial: Getting Started with MongoDB in Python
Hi everyone, welcome back. Python is a popular programming language that is widely used due to it’s many neat capabilities and features that can benefit a variety of different types of projects. MongoDB is also another popular choice for a document based database. MongoDB provides developers with more flexibility compared to relational databases. So we will go over how we can get started with MongoDB in Python in this article. With this introduction out of the way, let’s get into it.

Installations
First, we need to make sure we have two things installed. One is MongoDB itself and the other is PyMongo which is the driver that Python will use to interact with MongoDB.
A link to a local MongoDB instance can be found here. Or feel free to download and mess with other versions as you please.
PyMongo can be installed by using PIP or alternatively as a plugin through an IDE. The following command can be used to install PyMongo through PIP:
python -m pip install pymongo
How can we test if everything is installed and set up correctly?
Create a Python file and attempt to import PyMongo:
import pymongo
If there are no errors, then PyMongo should be successfully installed.
Creating a Database
Now let’s look into how we can create a database in MongoDB. We first need to connect to our MongoDB service by adding this line:
client = pymongo.MongoClient(port=27017)
MongoDB will then try to access the database with the given URL, if it is not found then it will treat it as creating a new database. For instance, let’s see an example if we want to create a new database named ‘testdatabase’:
import pymongo
client = pymongo.MongoClient(port=27017)
localdb = client['testdatabase']
Assuming we have no database named ‘testdatabase,’ MongoDB will look at it as a new connection. Note that MongoDB does not actually save a new database until one collection and document has been added into the collection. If you get any error during this stage, make sure that your MongoDB service is up and running.
Creating a Collection
Now let’s look into how to create a collection. If we want to create a new collection named ‘testcollection’ inside of our ‘testdatabase’ database, we can do so like this:
import pymongo
client = pymongo.MongoClient(port=27017)
localdb = client['testdatabase']
localcol = localdb['testcollection']
Similarly to creating a database, assuming there is no collection named ‘testcollection,’ MongoDB will look at it as a new connection. MongoDB will also not save a collection until a document has been added into it.
Inserting Data
Let’s insert some random data so we can save our newly created database and collection. We’ll create some junkdata and insert it into our collection:
import pymongo
client = pymongo.MongoClient(port=27017)
localdb = client['testdatabase']
localcol = localdb['testcollection']
junkdata = { "first": "one", "second": "two" }
insertedRecord = localcol.insert_one(junkdata)
And just like that, we have added our junkdata into our collection, ensuring that our newly created database and collection can be saved.
Verifying that our Database Exists
Now let’s see how we can check if a database exists. We can use the list_database_names() function to find a list of existing database names and check against it. Here is an example:
import pymongo
client = pymongo.MongoClient(port=27017)
list = client.list_database_names()
if "testdatabase" in list:
print('DB Exists')Output:
DB Exists
We checked if our database, ‘testdatabase,’ exists within the list of database names, and we got a match, meaning that it exists.
Verifying that our Collection Exists
Similarly to checking if a database exists, we can use the list_collection_names() function to find a list of collection within a database and check against it. Here is an example:
import pymongo
client = pymongo.MongoClient(port=27017)
localdb = client['testdatabase']
list = localdb.list_collection_names()
if "testcollection" in list:
print('Collection Exists')Output:
Collection Exists
We checked if our collection, ‘testcollection’, exists within the list of collection names found within the database, and we got a match, meaning that it exists.
Conclusion
This is the end of getting started with Mongo in Python. Hopefully this is enough to get you going. Python and Mongo are both great choice of a programming language and database. I hope this helps. Thanks for reading.