[JAVA-1515] Check if a database exists on MongoDB without using mongoClient.getDatabaseNames() Created: 21/Oct/14  Updated: 22/Oct/14  Resolved: 21/Oct/14

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 2.11.1
Fix Version/s: None

Type: New Feature Priority: Blocker - P1
Reporter: shiv Assignee: Ross Lawley
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

We configured our mongobd to forbid the "listdatabases" (i.e, mongClient.getDatabaseNames()) feature for privacy reasons

I would like to check whether a database exists in MongoDB without using mongClient.getDatabaseNames().

If I use, mongoClient.getDB("mydb"), mongodb creates a new db instance which can't help to check whether dbname exist.

Can we have an api to check existance of database (like : mongoClient.isDBExists("db_name") )?



 Comments   
Comment by shiv [ 22/Oct/14 ]

Thanks for clarifying

Comment by Ross Lawley [ 21/Oct/14 ]

Closing as this is not a Java driver issue but rather a mongodb configuration issue.

Comment by Ross Lawley [ 21/Oct/14 ]

The way to check if a database exists is to use the listdatabases command and see if the database is listed, there is no other command you can use. You could setup a user with a different mongodb role (with readAnyDatabase) and use that user to to call the command. You should still be able to assign roles to general users so that they can't use listdatabases and then meet your privacy requirements.

For future reference, the best place for general questions regarding MongoDB usage or the Java driver specifics is the mongodb-user mailinglist or stackoverflow as you will reach a boarder audience there.

Ross

Generated at Thu Feb 08 08:54:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.