MongoDB的连接字符串格式化为URI,详细信息可在此处找到.以下是基本格式和一些示例:
mongodb://[username:password@]host1[:port1][/[database][?options]] mongodb://127.0.0.1 mongodb://127.0.0.1/mydatabase mongodb://mongosrv.com:10230/mydatabase mongodb://myadmin:secretpass@mongosrv.com:10230/mydatabase // Or in your case it would be mongodb://ausername:apassword@flame.mongohq.com:27065/dunedin
您还可以使用MongoUrlBuilder和MongoUrl来构造或解析连接字符串programatticaly.虽然有点罗嗦,但我相信推荐用法是这样的
var mongoUrl = new MongoUrl(settings.ConnectionString); var mongoClient = new MongoClient(mongoUrl); var mongoServer = mongoClient.GetServer(); var mongoDatabase = mongoServer.GetDatabase(mongoUrl.DatabaseName);