I am consulting the databases of a mongo cluster and I need it to show me all the databases except some of them this way i get the databases in dictionary form:
for db in myclient.list_databases():
print(db)
``
`
and the output is as follows:
{'name': 'databasetest', 'sizeOnDisk': 32768.0, 'empty': False}
{'name': 'databaseQA', 'sizeOnDisk': 98304.0, 'empty': False}
{'name': 'databaseprod', 'sizeOnDisk': 32768.0, 'empty': False}
{'name': 'databasestage', 'sizeOnDisk': 32768.0, 'empty': False}
{'name': 'database2021', 'sizeOnDisk': 32768.0, 'empty': False}
{'name': 'database2022', 'sizeOnDisk': 32768.0, 'empty': False}
I would need the result to be all the db except:
{'name': 'database2021', 'sizeOnDisk': 32768.0, 'empty': False}
{'name': 'database2022', 'sizeOnDisk': 32768.0, 'empty': False}
how can i do it?
CodePudding user response:
You can use a list comprehension like this. This will be creating a new list named filtered_dbs that doesn't have the DB names you specified.
filtered_dbs = [db for db in myclient.list_databases() if db['name'] not in ['database2021', 'database2022']]
for db in filtered_dbs:
print(db)