Get the Names of all Collections using PyMongo - GeeksforGeeks

17 May, 2020
PyMongo is the module used for establishing a connection to the MongoDB using Python and perform all the operations like insertion, deletion, updating, etc. PyMongo is the recommended way to work with MongoDB and Python.
Note: For detailed information about Python and MongoDB visit MongoDB and Python.
Let’s begin with the Get Names of all Collections using PyMongo
Importing PyMongo Module: Import the PyMongo module using the command:from pymongo import MongoClientIf MongoDB is already not installed on your machine you can refer to the guide: Guide to Install MongoDB with PythonCreating a Connection: Now we had already imported the module, its time to establish a connection to the MongoDB server, presumably which is running on localhost (host name) at port 27017 (port number).client = MongoClient(‘localhost’, 27017)Accessing the Database: Since the connection to the MongoDB server is established. We can now create or use the existing database.mydatabase = client.name_of_the_databaseIn our case the name of the database is GeeksForGeeksmydatabase = client.GeeksForGeeksList the name of all the Collections in the Database: To list the name of all the collection in the database.mydatabase.collection_names()The collection_names() is deprecated in the version 3.7.0. Instead usemydatabase.list_collection_names()This method return the list of the collections in the Database.
Importing PyMongo Module: Import the PyMongo module using the command:from pymongo import MongoClientIf MongoDB is already not installed on your machine you can refer to the guide: Guide to Install MongoDB with Python
from pymongo import MongoClient
If MongoDB is already not installed on your machine you can refer to the guide: Guide to Install MongoDB with Python
Creating a Connection: Now we had already imported the module, its time to establish a connection to the MongoDB server, presumably which is running on localhost (host name) at port 27017 (port number).client = MongoClient(‘localhost’, 27017)
client = MongoClient(‘localhost’, 27017)
Accessing the Database: Since the connection to the MongoDB server is established. We can now create or use the existing database.mydatabase = client.name_of_the_databaseIn our case the name of the database is GeeksForGeeksmydatabase = client.GeeksForGeeks
mydatabase = client.name_of_the_database
In our case the name of the database is GeeksForGeeks
mydatabase = client.GeeksForGeeks
List the name of all the Collections in the Database: To list the name of all the collection in the database.mydatabase.collection_names()The collection_names() is deprecated in the version 3.7.0. Instead usemydatabase.list_collection_names()This method return the list of the collections in the Database.
mydatabase.collection_names()
The collection_names() is deprecated in the version 3.7.0. Instead use
mydatabase.list_collection_names()
This method return the list of the collections in the Database.
Example:
Sample Database:
# Python Program to demonstrate# List name of all collections using PyMongo      # Importing required librariesfrom pymongo import MongoClient    # Connecting to MongoDB server# client = MongoClient('host_name', 'port_number')client = MongoClient(‘localhost’, 27017)    # Connecting to the database named# GeeksForGeeksmydatabase = client.GeeksForGeeks    # Getting the names of all the collections# in GeeksForGeeks Database.collections = mydatabase.list_collection_names()    # Printing the name of the collections to the console.print(collections)
Output:
['Geeks']
Python-mongoDB
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
Python OOPs Concepts
Python | Get unique values from a list
Check if element exists in list in Python
Python Classes and Objects
Python | os.path.join() method
How To Convert Python Dictionary To JSON?
Python | Pandas dataframe.groupby()
Create a directory in Python