How to Use MongoDB Database in Python

Learn how to connect to a MongoDB database, list databases, collections, insert data into collections, fetching data from collections and more in Python using pymongo driver module.
  · 7 min read · Updated may 2022 · Database

Unlock the secrets of your code with our AI-powered Code Explainer. Take a look!

MongoDB is a cross-platform document-oriented database. It is classified as a NoSQL database, as it stores data in flexible, JSON-like documents, meaning fields can vary from document to another and the data structure can be changed over time.

Unlike relational databases such as MySQL that store data within tables, MongoDB stores data in collections in BSON format, which is a binary serialization of JSON, stands for "Binary JSON", anything you can do with JSON, you can do it with BSON as well, but with additional extensions that are not part of JSON, such as Date and BinData data types.

Related: How to Work with JSON Files in Python.

There are many reasons to choose MongoDB over SQL databases, including:

  • Making most of the cloud computing and storage
  • Flexibility
  • Speeding development
  • It is a distributed database, so high availability, horizontal scaling and geographic distribution are built in and easy to use.

Here is what we gonna cover in this tutorial:

Getting Started

First, you are going to need to install MongoDB in your machine, I highly suggest you check the official MongoDB's installation guide, you need to follow the procedure for your operating system.

Second, you need to install MongoDB Python driver: pymongo:

pip3 install pymongo

If you just install pymongo via pip, then you're good to go, the latest version of pymongo supports all MongoDB database versions.

Finally, You need to start the MongoDB daemon using the following command:

$ mongod

Connecting to a MongoDB Database

After you've installed MongoDB and started the Mongo daemon using mongod command, the below code is responsible for connecting to the database:

from pymongo import MongoClient
from pprint import pprint

# connect to the MongoDB server
client = MongoClient()
# or explicitly
# client = MongoClient("localhost", 27017)

Passing no parameters is the same as specifying localhost as host and the default port 27017 of MongoDB. Note that this will not make any connection yet, it will do only once you do any operation (such as getting server information).

If you want to get some information about the server, you can using client.server_info() method.

Listing Databases

Let's get all the databases available in our MongoDB server:

# list all database names
print("Available databases:", client.list_database_names())

This will only output the names for the databases, you can use client.list_databases() method if you want to get some information about databases such as size taken on disk, here is my output:

Available databases: ['admin', 'config', 'db', 'local', 'mydb']

Don't worry if you don't have the same output as mine, I had MongoDB for a while and for sure, I made a lot of work there.

Accessing a Database

To access a specific database, we can either by accessing it as an attribute or Python's dictionary-style:

# access the database "python", this will create the actual database
# if it doesn't exist
database = client["python"]
# or this:
# database = client.python

Note that the database does not exist, it'll be created automatically once we create a collection (like a table in SQL) and insert a document.

Listing All Collections with a Database

Collection is the term for a table in MongoDB. To get all the collections inside this database, we simply use database.list_collection_names() method:

# list all collections
print("Available collections:", database.list_collection_names())

Here is my output:

[]

Not very surprising, since this is a new database, we haven't created any collection yet. You can also use database.list_collections() method to get each collection along with some information.

Inserting Documents

A document in MongoDB is like a row in relational databases. For demonstration, let's make books collection and insert books in it:

# get books collection (or create one)
books = database["books"]
# insert a single book
result = books.insert_one({
    "name": "Invent Your Own Computer Games with Python, 4E",
    "author": "Al Sweigart",
    "price": 17.99,
    "url": "https://amzn.to/2zxN2W2"
})

We have retrieved books collection (or create it if it doesn't exist) and used books.insert_one() method to insert a single document, we simply passed a Python dictionary.

Let's get the ID of the inserted element:

print("One book inserted:", result.inserted_id)

Output:

One book inserted: 5ee79b10c6bd9552e204a625

A great feature about MongoDB, it automatically generates a unique ID for each element inserted, we'll see it again later on.

Now what if we want to insert multiple documents in a time ? We simply use insert_many() method instead of insert_one():

# insert many books
books_data = [
    {
        "name": "Automate the Boring Stuff with Python: Practical Programming for Total Beginners",
        "author": "Al Sweigart",
        "price": 17.76,
        "url": "https://amzn.to/2YAncdY"
    },
    {
        "name": "Python Crash Course: A Hands-On, Project-Based Introduction to Programming",
        "author": "Eric Matthes",
        "price": 22.97,
        "url": "https://amzn.to/2yQfQZl"
    },
    {
        "name": "MySQL for Python",
        "author": "Albert Lukaszewski",
        "price": 49.99,
    }
]
result = books.insert_many(books_data)
print("Many books inserted, Ids:", result.inserted_ids)

We have inserted 3 documents in one go, notice the last book doesn't have a url field and MongoDB doesn't complain about it, because it's allowed!

Here is my output:

Many books inserted, Ids: [ObjectId('5ee79c4fc6bd9552e204a62c'), ObjectId('5ee79c4fc6bd9552e204a62d'), ObjectId('5ee79c4fc6bd9552e204a62e')]

Fetching Documents

The amazing thing about MongoDB is that we can filter documents using a Python dictionary, let's get a single book by a specific author:

# get a single book by a specific author
eric_book = books.find_one({"author": "Eric Matthes"})
pprint(eric_book)

Output:

{'_id': ObjectId('5ee79c10c6bd9552e204a627'),
 'author': 'Eric Matthes',
 'name': 'Python Crash Course: A Hands-On, Project-Based Introduction to '
         'Programming',
 'price': 22.97,
 'url': 'https://amzn.to/2yQfQZl'}

The output is a Python dictionary as well, let's get all books of Al Sweigart:

# get all books by a specific author
sweigart_books = books.find({"author": "Al Sweigart"})
print("Al Sweigart's books:")
pprint(list(sweigart_books))

Notice I wrapped the result in a list() function to retrieve it as a list of dictionaries, that's because find() method returns a pymongo.cursor.Cursor() instance, in which you can iterate over it using a for loop, or wrap it using list() function. Here is the expected output:

[{'_id': ObjectId('5ee79b10c6bd9552e204a625'),
  'author': 'Al Sweigart',
  'name': 'Invent Your Own Computer Games with Python, 4E',
  'price': 17.99,
  'url': 'https://amzn.to/2zxN2W2'},
 {'_id': ObjectId('5ee79c10c6bd9552e204a626'),
  'author': 'Al Sweigart',
  'name': 'Automate the Boring Stuff with Python: Practical Programming for '
          'Total Beginners',
  'price': 17.76,
  'url': 'https://amzn.to/2YAncdY'},
 {'_id': ObjectId('5ee79c34c6bd9552e204a629'),
  'author': 'Al Sweigart',
  'name': 'Automate the Boring Stuff with Python: Practical Programming for '
          'Total Beginners',
  'price': 17.76,
  'url': 'https://amzn.to/2YAncdY'},
 {'_id': ObjectId('5ee79c4fc6bd9552e204a62c'),
  'author': 'Al Sweigart',
  'name': 'Automate the Boring Stuff with Python: Practical Programming for '
          'Total Beginners',
  'price': 17.76,
  'url': 'https://amzn.to/2YAncdY'}]

Finally, if you want to get all the documents without any filter, you can simply pass an empty dictionary to find() method:

# get all documents in books collection
all_books = books.find({})
print("All books:")
pprint(list(all_books))

Deleting a Document

To delete a specific document, you simply use delete_one() method, here is an example:

# delete a specific document by a JSON query
result = books.delete_one({"author": "Albert Lukaszewski"})

Even if there is more than one document using that filter, it will still delete a single document. If you want to delete all the documents using that filter, you use delete_many() method:

# delete all books by Al Sweigart
result = books.delete_many({"author": "Al Sweigart"})

Deleting a Collection

To delete a collection in MongoDB, you have two options:

# drop this collection
database.drop_collection("books")
# or this:
# books.drop()

This will delete books collection.

Deleting a Database

# drop this entire database
client.drop_database("python")
# close the connection
client.close()

drop_database() does what its name suggests, it deletes the database given the name in the arguments.

Finally, we close the connection using close() method.

Conclusion

Now you know the basic functionalities of MongoDB and its Python driver, pymongo. I encourage you to play around with it to get familiar with the database, it's as flexible as Python language!

Related: How to Use MySQL Database in Python.

Happy Coding ♥

Finished reading? Keep the learning going with our AI-powered Code Explainer. Try it now!

View Full Code Assist My Coding
Sharing is caring!



Read Also



Comment panel

    Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!