balexandre
balexandre

Reputation: 75073

How to create a new Mongo Database programatically?

I'm trying to create a new database programatically, kind'a one database per client.

and using this:

public void CreateNewClientDatabase(Client client)
{
    var connectionString = Util.GetClientDatabaseConnectionString(client.DatabaseName);
    var mongoClient = new MongoDB.Driver.MongoClient(connectionString);
    var server = mongoClient.GetServer();

    var db = server.GetDatabase(client.DatabaseName);
    db.CreateCollection("DatabaseCreated");
}

The Error I'm getting on CreateCollection is that I do not have the correct credentials, even though that in the connection string, my credentails are correct.

The Exception reads as:

Invalid credentials for database 'client_database_name'.

and the InnerException as:

{"Command 'authenticate' failed: auth fails (response: { \"errmsg\" : \"auth fails\", \"ok\" : 0.0 })"}

The connectionString ends up being this:

mongodb://admin_user:[email protected]:10042/client_database_name

What am I missing?


P.S. Using version 1.7 of MongoDB Driver

Upvotes: 2

Views: 1431

Answers (1)

Jason McCay
Jason McCay

Reputation: 3345

Bruno ... to do this on a shared service like MongoHQ, you will need to use their API to create new databases programmatically. Docs for the API are located at: http://support.mongohq.com.

Upvotes: 2

Related Questions