[CSHARP-1355] Can't Create Database Created: 21/Jul/15  Updated: 22/Jul/15  Resolved: 21/Jul/15

Status: Closed
Project: C# Driver
Component/s: Operations
Affects Version/s: 2.0.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Hu Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDB 3.1.5



 Description   

I want to create a database via C#
the code can't work as below:
var db = mongoSvr.GetDatabase(dbName);

but if I create a collection just after GetDatabase,new database and collection is append to the server.



 Comments   
Comment by Craig Wilson [ 22/Jul/15 ]

Hi Hu,

I understand your use case. However, there are not any drivers that can actually create a database without creating a collection and/or inserting a data into that collection. In both 1.x and 2.x, the GetDatabase methods only create a new instance of a local database object and return it: https://github.com/mongodb/mongo-csharp-driver/blob/v1.x/MongoDB.Driver/MongoServer.cs#L667 and https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver/MongoClient.cs#L115.

So, while it may feel dirty (and I agree), if your need is to actually create the database, you'll need to create a collection and then drop it.

Craig

Comment by Hu [ 22/Jul/15 ]

Hi Wilson

Thank you for your reply.
But in my case, the application is a Administrator tool,
Sometimes, administrator only want to create a database without any collection.After create database , someone else will create collection.
A solution is after GetDatabase , a dummy collection is created,then the dummy collection is dropped.But I think it's smell bad.
By the way, driver 1.x ,GetDatabase method really create new database if the database is not exist.

Comment by Craig Wilson [ 21/Jul/15 ]

Hi Hu,

Databases are created on first use. Simply calling GetDatabase doesn't create the database. If you want to explicitly create the database, you'll need to create a collection, or insert some data.

Craig

Generated at Wed Feb 07 21:39:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.