Add support for db.exists() and collections.exists()
Add a db.exists() and collections.exists() so that it is easy to check whether a database or a collection exists. This will help to reduce the propensity to inadvertently create new dbs and collections when running a script due to typos.
![](https://secure.gravatar.com/avatar/a953ce375bdf82ec7e3d014849f4c769?size=40&default=https%3A%2F%2Fassets.uvcdn.com%2Fpkg%2Fadmin%2Ficons%2Fuser_70-6bcf9e08938533adb9bac95c3e487cb2a6d4a32f890ca6fdc82e3072e0ea0368.png)
-
Wernfried commented
Can be done with a single command:
db.exists() -> db.adminCommand( { listDatabases: 1 , filter: { name: 'db' } } ).databases.length > 0
and
collections.exists() -> db.getCollectionNames().includes('collections')
-
Frederic commented
This is the re-created version of https://jira.mongodb.org/browse/SERVER-1938?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel&focusedCommentId=3611783#comment-3611783 where a lot of discussion has already taken placE.