[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# 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. |
| 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 |