[英]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 )
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.