Creating a database from the 10gen C# driver

1,666 views
Skip to first unread message

ALH

unread,
Jan 18, 2012, 11:05:34 AM1/18/12
to mongodb-user
How do I create a database from using 10gen C# driver? I have tried
this using the GetDatabase method in MongoServer with no luck. Also,
how would I do this if running in authentication mode? Is it any
different then running within a replica set? Thanks.

Robert Stam

unread,
Jan 18, 2012, 11:11:58 AM1/18/12
to mongod...@googlegroups.com
The database will be created automatically when you insert the first document.

When running in authentication mode you must provide the credentials to use for that database, like this:

var credentials = new MongoCredentials("username", "password");
var database = server.GetDatabase("databaseName", credentials);

The only thing different about running with a replica set is that your connection string must have a seed list containing all or some of the replica set members.


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


ALH

unread,
Jan 27, 2012, 7:22:03 AM1/27/12
to mongodb-user
Hi Robert,

Thanks for the reply. I have ran into some issues with the suggested
approach that I would like to share with you. So, just to recap:

I want to create a new database and then a new collection in that
database.

Here is what I am doing in code to achieve the above:

// Connect to server
var url = "mongodb://someAdminUser:someAdminUserPassword@localhost:
9001/admin"
var server = MongoServer.Create(url);

// Create my new database
var db = server.GetDatabase("SomeNewDatabase");

// Create my new collection
var collection = db.CreateCollection("MyNewCollection");


Observations:

I am running in authenticated mode but the database has not been
create yet, therefore, has no user credentials to authenticate against


Questions:

1. Given that I am running in authenticated mode what is the proper
way to connect to the server so that I can create a database that at
creation will not have any credentials? (please see the below error
message I am getting when executing the above code sample)
2. Is there a way to create a database without writing any data like
you would in SQL Server? I am currently doing this from the mongo
shell in PowerShell using connect("MyNewDatabase"). I would be willing
to write a CreateDatabase method if you can provide guidance.


Invalid credentials for database 'SomeTestDatabase'.
at MongoDB.Driver.Internal.MongoConnection.Authenticate(String
databaseName, MongoCredentials credentials) in C:\work\10gen\mongodb
\mongo-csharp-driver\Driver\Internal\MongoConnection.cs:line 165
at
MongoDB.Driver.Internal.MongoConnection.CheckAuthentication(MongoDatabase
database) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal
\MongoConnection.cs:line 244
at
MongoDB.Driver.MongoServerInstance.AcquireConnection(MongoDatabase
database) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core
\MongoServerInstance.cs:line 260
at MongoDB.Driver.MongoServer.AcquireConnection(MongoDatabase
database, Boolean slaveOk) in C:\work\10gen\mongodb\mongo-csharp-driver
\Driver\Core\MongoServer.cs:line 1052
at MongoDB.Driver.MongoCursorEnumerator`1.AcquireConnection() in C:
\work\10gen\mongodb\mongo-csharp-driver\Driver\Core
\MongoCursorEnumerator.cs:line 184
at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst() in C:\work
\10gen\mongodb\mongo-csharp-driver\Driver\Core
\MongoCursorEnumerator.cs:line 194
at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext() in C:\work
\10gen\mongodb\mongo-csharp-driver\Driver\Core
\MongoCursorEnumerator.cs:line 126
at MongoDB.Driver.MongoDatabase.GetCollectionNames() in C:\work
\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoDatabase.cs:line
662
at MongoDB.Driver.MongoDatabase.CollectionExists(String
collectionName) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver
\Core\MongoDatabase.cs:line 305

Thanks.

Best Regards,
RelayHealth
Albert L. Hives

Robert Stam

unread,
Jan 27, 2012, 10:07:14 AM1/27/12
to mongodb-user
If you want the default credentials supplied in the URL to be
authenticated against the admin database you put "(admin)" after the
username in the URL, like this:

var url = "mongodb://
someAdminUser(admin):someAdminUserPassword@localhost:9001"

When you authenticate against the admin database you gain access to
all databases at once (including the new one you are about to create).

ALH

unread,
Jan 27, 2012, 11:58:53 AM1/27/12
to mongodb-user
Hi Robert,

This totally worked though I could not find this connection string on
the driver docs @ http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-TheC%23Driver

Robert Stam

unread,
Jan 27, 2012, 12:05:31 PM1/27/12
to mongod...@googlegroups.com

ALH

unread,
Jan 27, 2012, 2:06:39 PM1/27/12
to mongodb-user
You are correct. I see it now. Thanks again. :)

On Jan 27, 9:05 am, Robert Stam <rob...@10gen.com> wrote:
> It's kind of buried in there:
>
> http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriv...
>
>
>
>
>
>
>
> On Fri, Jan 27, 2012 at 11:58 AM, ALH <ahi...@gmail.com> wrote:
> > Hi Robert,
>
> > This totally worked though I could not find this connection string on
> > the driver docs @
> >http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriv...
Reply all
Reply to author
Forward
0 new messages