Get MongoDB Database And Collection Names with PyMongo and Python

Introduction

MongoDB stores its data (known as “documents”) by grouping them in collections that are themselves stored in a database. This article will help you to create database and collection names for a MongoDB server, and then it will help you to retrieve the database and collection names, in Python, with some example code.

Prerequisites to using PyMongo

Python 3 and its PIP package manager need to be installed on the machine that will make API requests to the MongoDB server. Use the following commands to ensure that they’re installed:

pip3 -V
python3 -V

You can install the pymongo Python distribution for MongoDB using the pip3 install command:

pip3 install pymongo

Screenshot of terminal returning mongo, Python 3, and PIP3 version, and installing pymongo

The MongoDB service, and mongo Shell, need to be installed on your machine as well. You can use the mongo --version command to verify that it’s installed.

Create collections for a MongoDB database

You should have some collections and databases on your MongoDB server that you can use with the Python code found in this article. Here are two ways you can create some collections for your MongoDB document data.

Create some collections using the MongoDB Compass UI

If you have the MongoDB Compass graphical interface installed on your machine you can use it to create some collections.

Create a new database (if you don’t have one created already) by clicking the plus sign (+) at the bottom left-hand side of the GUI application:

Screenshot of MongoDB Compass UI creating a database

Once you’ve created a new database you can click the plus sign (+), next to the database name on the left-hand side, to create a new collection:

Screenshot of MongoDB Compass UI creating a collection

NOTE: MongoDB collection names may contain spaces, but the database name cannot.

Create a MongoDB collection in the mongo Shell command line

Another way to create a collection is in the mongo Shell command-line interface by typing the mongo command in a terminal window. Once you’re inside the mongo Shell type the use command to connect to a database:

use some_database

It should respond by repeating the database name. At this point the db keyword will serve a substitute variable for the database. You can create a collection for the database by inserting data into a collection attribute of db like in the following example:

db["Collection 3"].insert( {"Key 1": "Value goes here"} )

If the collection name has no spaces in it you can also create it using the following example code:

db.collection_4.insert( {"Key 1": "Value goes here"} )

Use the show command inside mongo to have it return all of the collections on your current database:

show collections

Screenshot of mongo Shell using a database and creating a collection after data insertion

NOTE: Just type exit and press Return to quit the mongo Shell.

Use Python and PyMongo to return all of the MongoDB databases and collection

Now that you have some collections, and at least one database, you can use the PyMongo distribution for MongoDB to return them, and strings, in a Python script.

Create a Python script and import PyMongo

Use the touch command in a terminal window to create a new Python script (with the .py file extension), or you can also use an IDE with Python support, like Visual Studio Code, to create a new file.

At the top of your script make sure to import the necessary PyMongo libraries, and instantiate a string and integer object for the host parameter’s domain and port values.

The following code imports the MongoClient and errors libraries from pymongo:

# import the MongoClient class
from pymongo import MongoClient, errors

The next step is to declare a string and integer for the MongoClient() domain URL and port number respectively:

# default port is 27017
DOMAIN = 'localhost:'
PORT = 27017

Make sure that you change the values in DOMAIN and PORT so that they match your server’s settings.

Create a client object instance using the MongoClient() PyMongo method

The following code attempts to connect to the MongoDB server inside of a try-except indentation block by concatenating a string, from the host variables declared earlier, and then passing it to the MongoClient() method call:

# use a try-except indentation to catch MongoClient() errors
try:
    # try to instantiate a client instance
    client = MongoClient(
        host = [ str(DOMAIN) + str(PORT) ],
        serverSelectionTimeoutMS = 3000 # 3 second timeout
    )

    # print the version of MongoDB server if connection successful
    print ("server version:", client.server_info()["version"])

except errors.ServerSelectionTimeoutError as err:
    # set the client instance to 'None' if exception
    client = None

    # catch pymongo.errors.ServerSelectionTimeoutError
    print ("pymongo ERROR:", err)

The code above uses the serverSelectionTimeoutMS parameter in order to attempt to attempt the connection for 3 seconds. If an exception is returned by pymongo, or if the connection otherwise fails, then the client object instance of MongoClient() will be set to None.

Get the database and collection names using the

If the connection was successful then the following code will return all of the database names for the MongoDB connection:

if client != None:

    # the list_database_names() method returns a list of strings
    database_names = client.list_database_names()

    print ("database_names() TYPE:", type(database_names))
    print ("The client's list_database_names() method returned", len(database_names), "database names.")

Use Python’s enumerate() function to iterate over the database names

You can then iterate over the list of database_names names returned by the list_database_names() method, to get the string name for each database. The following code uses enumerat() to iterate over each name and calls the list_collection_names() method to get all of the collection names for the respective database:

    # iterate over the list of database names
    for db_num, db in enumerate(database_names):

        # print the database name
        print ("\nGetting collections for database:", db, "--", db_num)

        # use the list_collection_names() method to return collection names
        collection_names = client[db].list_collection_names()
        print ("list_collection_names() TYPE:", type(database_names))
        print ("The MongoDB database returned", len(collection_names), "collections.")

Iterate over the MongoDB collection names in Python

The list_collection_names() method should also return a list of string names. The following code is another iterator, nested inside of the iterator for the collection names, that will print the name for each collection:

        # iterate over the list of collection names
        for col_num, col in enumerate(collection_names):
            print (col, "--", col_num)

NOTE: The database_names() and collection_names() methods for the MongoClient library are now deprecated in newer versions of PyMongo.

Conclusion

Make sure to save the code inside of the Python script and then you can run it using the python3 command for Python 3.x:

python3 get_db_col_names.py

Screenshot of a Python script running in a terminal window to get MongoDB database and collection names

The tutorial and example code in this article should have given you a good introduction to MongoDB database and collection names, and how you can use the PyMongo distribution for MongoDB to retrieve the strings names for the databases and collection in a Python script.

Just the Code

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

# import the MongoClient class
from pymongo import MongoClient, errors

# default port is 27017
DOMAIN = 'localhost:'
PORT = 27017

# use a try-except indentation to catch MongoClient() errors
try:
    # try to instantiate a client instance
    client = MongoClient(
        host = [ str(DOMAIN) + str(PORT) ],
        serverSelectionTimeoutMS = 3000 # 3 second timeout
    )

    # print the version of MongoDB server if connection successful
    print ("server version:", client.server_info()["version"])
except errors.ServerSelectionTimeoutError as err:
    # set the client instance to 'None' if exception
    client = None

    # catch pymongo.errors.ServerSelectionTimeoutError
    print ("pymongo ERROR:", err)

"""
database_names() AND collection_names ARE DEPRECATED
"""

if client != None:

    # the list_database_names() method returns a list of strings
    database_names = client.list_database_names()

    print ("database_names() TYPE:", type(database_names))
    print ("The client's list_database_names() method returned", len(database_names), "database names.")

    # iterate over the list of database names
    for db_num, db in enumerate(database_names):

        # print the database name
        print ("\nGetting collections for database:", db, "--", db_num)

        # use the list_collection_names() method to return collection names
        collection_names = client[db].list_collection_names()
        print ("list_collection_names() TYPE:", type(database_names))
        print ("The MongoDB database returned", len(collection_names), "collections.")

        # iterate over the list of collection names
        for col_num, col in enumerate(collection_names):
            print (col, "--", col_num)

else:
    print ("The domain and port parameters passed to client's host is invalid")

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.