我正在尝试连接到CentOS上的虚拟机中的mongodb(配置了selinux和防火墙)。我创建了一个名为test的数据库,其中包含一个名为test的集合和一个查看文档。 我使用C#和最新的驱动程序2.0.0来连接数据库。 代码如下所示:
using System;
using System.Dynamic;
using System.Collections.Generic;
using MongoDB.Driver;
using MongoDB.Bson;
namespace mongotest
{
class MainClass
{
public static void Main (string[] args)
{
MongoClientSettings settings = new MongoClientSettings ();
settings.Server = new MongoServerAddress ("mongotest", 21017);
MongoClient client = new MongoClient (settings);
System.Threading.Tasks.Task<List<BsonDocument>> datasetsTask = client.GetDatabase ("test").GetCollection<BsonDocument> ("test").Find (x => true).ToListAsync();
foreach (BsonDocument dataset in datasetsTask.Result) {
try {
Console.WriteLine(dataset["_id"]);
try{
Console.WriteLine("\t" + dataset["test"]);
} catch(Exception e) {}
try{
Console.WriteLine("\t" + dataset["name"]);
} catch(Exception e) {}
} catch(Exception e) {Console.WriteLine ("Error: no property called _id");}
}
}
}
}
我用robomongo检查了数据库。那里有测试数据库和测试集合。在我尝试获取结果的行中(在foreach循环的头部)执行暂停30秒然后抛出System.TimeoutException(我不得不将它发布到pastebin中,因为它太长了):{{3 }}
第58行中的下行是我认为的真正错误。但我不知道这个服务器地址是怎么回事。
答案 0 :(得分:0)
看起来连接有问题。
尝试设置连接,就像Github页面上的“入门”示例一样:
https://github.com/mongodb/mongo-csharp-driver
另外,请记住将“mongodb://”添加到连接字符串中。