Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if mongodb database exists?

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

like image 311
MnomrAKostelAni Avatar asked Aug 13 '11 09:08

MnomrAKostelAni


People also ask

How can I tell if a database is created in MongoDB?

ASP.NET Core 3 MVC Application with MongoDB There are two possibilities to check if MongoDB database exists. Case 1: The first possibility is that the MongoDB database exists i.e. it returns particular index. Case 2: The second possibility is that the MongoDB database does not exist i.e. it returns index -1.

What does find () do in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents.

Is null in MongoDB?

The { item : null } query matches documents that either contain the item field whose value is null or that do not contain the item field. The query returns both documents in the collection.


5 Answers

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();

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

like image 166
Jared Avatar answered Oct 29 '22 09:10

Jared


From the shell, if you want to explicitely check that a DB exists:

db.getMongo().getDBNames().indexOf("mydb");

Will return '-1' if "mydb" does not exist.

To use this from the shell:

if [ $(mongo localhost:27017 --eval 'db.getMongo().getDBNames().indexOf("mydb")' --quiet) -lt 0 ]; then
    echo "mydb does not exist"
else
    echo "mydb exists"
fi
like image 39
leucos Avatar answered Oct 29 '22 09:10

leucos


For anyone who comes here because the method getDatabaseNames(); is depreciated / not available, here is the new way to get the list of existing databases:

MongoClient mongoClient = new MongoClient();
MongoCursor<String> dbsCursor = mongoClient.listDatabaseNames().iterator();
while(dbsCursor.hasNext()) {
    System.out.println(dbsCursor.next());
}

Here is a method that validates if the database is found:

public Boolean databaseFound(String databaseName){
    MongoClient mongoClient = new MongoClient(); //Maybe replace it with an already existing client
    MongoCursor<String> dbsCursor = mongoClient.listDatabaseNames().iterator();
    while(dbsCursor.hasNext()) {
        if(dbsCursor.next().equals(databaseName))
            return true;
    }
    return false;
}
like image 4
GammaOmega Avatar answered Oct 29 '22 07:10

GammaOmega


in python using Pymongo

from pymongo import MongoClient

db_name = "foo"
conn = MongoClient('mongodb://localhost,localhost:27017')
db = self.conn[str(db_name)]
if bool(db_name in conn.database_names()):
   collection.drop()
like image 2
c24b Avatar answered Oct 29 '22 08:10

c24b


using MongoDb c# Driver 2.4

    private bool DatabaseExists(string database)
    {
       // _client is IMongoClient
        var dbList = _client.ListDatabases().ToList().Select(db => db.GetValue("name").AsString);
        return dbList.Contains(database);
    }

usage:

        if (!DatabaseExists("FooDb")
        {
            // create and seed db

        }
like image 2
Purusartha Avatar answered Oct 29 '22 07:10

Purusartha