Check if mongodb database exists?

前端 未结 9 1495
孤街浪徒
孤街浪徒 2020-12-14 21:58

Is there a possibility to check if a mongo database allready exists?

9条回答
  •  眼角桃花
    2020-12-14 22:47

    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 databaseNames = mongo.getDatabaseNames();
    

    This is equivalent to the mongo shell "show dbs" command. I am sure similar methods exist in all of the drivers.

提交回复
热议问题