简体   繁体   English

如何在C#中获取连接字符串中指定的Mongo数据库

[英]How to get the Mongo database specified in connection string in C#

I would like to connect to the database specified in the connection string, without specifying it again in GetDatabase . 我想连接到连接字符串中指定的数据库,而不是在GetDatabase再次指定它。

For example, if I have a connection string like this; 例如,如果我有这样的连接字符串;

mongodb://localhost/mydb

I would like to be able to db.GetCollection("mycollection") from mydb . 我希望能够从mydb获得db.GetCollection("mycollection")

This would allow the database name to be configured easily in the app.config file. 这将允许在app.config文件中轻松配置数据库名称。

Update: 更新:

MongoServer.Create is obsolete now (thanks to @aknuds1). MongoServer.Create现在已经过时了(感谢@ aknuds1)。 Instead this use following code: 相反,这使用以下代码:

var _server = new MongoClient(connectionString).GetServer();

It's easy. 这很简单。 You should first take database name from connection string and then get database by name. 您应首先从连接字符串中获取数据库名称,然后按名称获取数据库。 Complete example: 完整的例子:

var connectionString = "mongodb://localhost:27020/mydb";

//take database name from connection string
var _databaseName = MongoUrl.Create(connectionString).DatabaseName;
var _server = MongoServer.Create(connectionString);

//and then get database by database name:
_server.GetDatabase(_databaseName);

Important : If your database and auth database are different, you can add a authSource= query parameter to specify a different auth database. 要点 :如果数据库和auth数据库不同,则可以添加authSource = query参数以指定其他auth数据库。 (thank you to @chrisdrobison ) (谢谢@chrisdrobison

From docs: 来自docs:

NOTE If you are using the database segment as the initial database to use, but the username and password specified are defined in a different database, you can use the authSource option to specify the database in which the credential is defined. 注意如果要将数据库段用作要使用的初始数据库,但指定的用户名和密码是在其他数据库中定义的,则可以使用authSource选项指定定义凭据的数据库。 For example, mongodb://user:pass@hostname/db1?authSource=userDb would authenticate the credential against the userDb database instead of db1. 例如,mongodb:// user:pass @ hostname / db1?authSource = userDb将根据userDb数据库而不是db1对凭证进行身份验证。

In this moment with the last version of the C# driver (2.3.0) the only way I found to get the database name specified in connection string is this: 在这一刻,使用最新版本的C#驱动程序(2.3.0),我发现获取连接字符串中指定的数据库名称的唯一方法是:

var connectionString = @"mongodb://usr:pwd@srv1.acme.net,srv2.acme.net,srv3.acme.net/dbName?replicaSet=rset";
var mongoUrl = new MongoUrl(connectionString);
var dbname = mongoUrl.DatabaseName;
var db = new MongoClient(mongoUrl).GetDatabase(dbname);
db.GetCollection<MyType>("myCollectionName");

With version 1.7 of the official 10gen driver, this is the current (non-obsolete) API: 使用官方10gen驱动程序的1.7版本,这是当前(非过时的)API:

const string uri = "mongodb://localhost/mydb";
var client = new MongoClient(uri);
var db = client.GetServer().GetDatabase(new MongoUrl(uri).DatabaseName);
var collection = db.GetCollection("mycollection");

The answer below is apparently obsolete now, but works with older drivers. 下面的答案显然已经过时,但适用于较老的司机。 See comments. 看评论。

If you have the connection string you could also use MongoDatabase directly: 如果您有连接字符串,您也可以直接使用MongoDatabase:

var db =  MongoDatabase.Create(connectionString);
var coll = db.GetCollection("MyCollection");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过在 mongo C# 驱动程序中仅使用连接字符串来`ListCollections`? - How to `ListCollections` by using only connection string in mongo C# driver? c#数据库连接字符串 - c# database connection string C#SQL Server的使用 <Database> &#39;在连接字符串中指定“初始目录”时需要 - C# SQL Server 'Use <Database>' Required when "Initial Catalog' specified in COnnection String 如何在运行时以编程方式从本地数据库获取连接字符串? c# winforms - how to programmatically get connection string from local database at run time? c# winforms 如何通过C#中指定的FileStream.Position和length获取字符串 - How to get string by FileStream.Position and length specified in c# 如何创建动态数据库连接字符串C# - How to create dynamic database connection string C# 如何使用 C# 中的连接字符串访问 Azure SQL 数据库? - How to access Azure SQL database with a Connection String in C#? 如何将文本框文本详细信息传递给数据库连接字符串C# - how to pass textbox text details to database connection string c# 如何解决连接字符串的异常(本地数据库C#) - how to solve exception for the connection string(local database c#) C#应用程序的个性化数据库连接字符串 - Personalized database Connection string for C# application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM