检查mongodb数据库是否存在?
问题描述:
有可能检查一个mongo数据库是否已经存在?
Is there a possibility to check if a mongo database allready exists?
答
数据库。从Java驱动程序,你可以这样做,以获取在localhost上运行的 mongod 服务器上的数据库名称
Yes, you can get the list of existing databases. From the Java driver you could do something like this to get the database names on a mongod server running on localhost
Mongo mongo = new Mongo( "127.0.0.1", 27017 );
List<String> databaseNames = mongo.getDatabaseNames();
这相当于mongo shell的show dbs命令。我确信所有的驱动程序都存在类似的方法。
This is equivalent to the mongo shell "show dbs" command. I am sure similar methods exist in all of the drivers.