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.
-
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.