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 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)
- 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_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.
The collection_names() is deprecated in the version 3.7.0. Instead use
This method return the list of the collections in the Database.
- Get all the information of a Collection's indexes using PyMongo
- Get all the Documents of the Collection using PyMongo
- Update all Documents in a Collection using PyMongo
- How to rebuild all the indexes of a collection using PyMongo?
- How to Drop all the indexes in a Collection using PyMongo?
- How to get column names in Pandas dataframe
- How to get rows/index names in Pandas dataframe
- How to get column and row names in DataFrame?
- Difference Between insert(), insertOne(), and insertMany() in Pymongo
- Convert PyMongo Cursor to JSON
- Convert PyMongo Cursor to Dataframe
- Nested Queries in PyMongo
- What is a PyMongo Cursor?
- How to check if the PyMongo Cursor is Empty?
- Rename all file names in your directory using Python
- PyQt5 QSpinBox - Getting all the properties names
- PyQt5 QCalendarWidget - Getting all the Property names
- Count frequencies of all elements in array in Python using collections module
- Python | Change column names and row indexes in Pandas DataFrame
- How to lowercase column names in Pandas dataframe
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.