Home Code List all databases in MongoDB using PyMongo

List all databases in MongoDB using PyMongo

Script to list the names of all MongoDB databases using PyMongo.

from pymongo import MongoClient

#connect to local mongodb instance
client = MongoClient('localhost',27017)

#get the list of all MongoDB databases in the server
db_list = [db_name for db_name in client.database_names()]

List non-default databases in MongoDB

If you do not want the default MongoDB databases (admin, local) to be included in the database names list, then modify the above script as follows.

from pymongo import MongoClient

#connect to local mongodb instance
client = MongoClient('localhost',27017)

#get the list of all MongoDB databases in the server except the default databases
db_list = [db_name for db_name in client.database_names() if db_name not in ('admin','local')]
Content Protection by DMCA.com
Gurudevhttps://www.hitxp.com
Gurudev is the developer of Gurunudi AI Platform. This is his official website where he pens his thoughts on a wide range of topics, answers queries, shares resources and tools developed by him.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,248FansLike
10FollowersFollow
153FollowersFollow
377FollowersFollow

Subscribe to HitXP Articles

To be updated with all the latest articles, offers and special announcements.

Latest Articles

An Efficient Strategy to fight Corona Virus

By setting up local police station level online delivery networks for essential items like grocery, medicines - the need for general public to come out of their homes can be removed.

Rising pollution can cause Pandemics deadlier than Corona Virus

Rising pollution can expose ancient killer microbes that may cause pandemics more deadlier than Corona. Melting Ice Sheets can resurrect killer agents of the past.

Corona Virus, SARS – Why are new viruses increasingly originating from China?

The reason why new strains of deadly viruses are emerging from China. Viruses like coronavirus and SARS should be stopped from evolving and spreading to prevent another global pandemic.

Evolution of life – primitive cells, complex organisms, intelligence, what next after humans?

Evolution of life from primitive life forms to complex multi cellular organisms to intelligent human species - the advent of AI raises a natural question - What next after human?
Content Protection by DMCA.com