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:
1 2 | pip3 -V python3 -V |
You can install the pymongo
Python distribution for MongoDB using the pip3 install
command:
1 | pip3 install 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:
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:
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:
1 | 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:
1 | 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:
1 | 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:
1 | show collections |
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
:
1 2 | # 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:
1 2 3 | # 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # 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:
1 2 3 4 5 6 7 | 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:
1 2 3 4 5 6 7 8 9 10 | # 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:
1 2 3 | # 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:
1 | python3 get_db_col_names.py |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #!/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