【发布时间】:2015-10-06 09:27:09
【问题描述】:
这是完整的程序代码: (除了只有 id 和用户名的 Follower 类 - Json 类型的类) 我没有任何 db 用户 - 只是创建了简单的集合
class Program
{
static void Main(string[] args)
{
DBClass db = new DBClass();
db.check();
}
}
class DBClass
{
protected string connectionString ="mongodb://localhost:27017";
protected IMongoClient _client;
protected IMongoDatabase _database;
protected IMongoCollection<Follower> collection;
public DBClass()
{
_client = new MongoClient(connectionString);
_database = _client.GetDatabase("FollowedUsers");
collection = _database.GetCollection<Follower>("UserData");
}
public async void check()
{
var filter = Builders<Follower>.Filter.Eq("id", "266663861");
var result = await collection.Find(filter).CountAsync();
Console.WriteLine(result);
Console.ReadLine();
}
}
}
这是我在 mongod 中看到的:
2015-10-06T12:08:24.119+0300 I NETWORK [initandlisten] connection
accepted from 127.0.0.1:50069 #2 (1 connection now open)
2015-10-06T12:08:24.243+0300 I NETWORK [conn2] end connection
27.0.0.1:50069 (0 connections now open)
当我从 VS 运行它时,它只是终止,不抛出异常,这就是我在输出中看到的内容 程序“[11724] MongoTest.vshost.exe”已退出,代码为 0 (0x0)。 当我调试它时,我看到它在这一行终止:
var result = await collection.Find(filter).CountAsync();
我是不是配置错了?
【问题讨论】:
-
1.更改检查以返回任务...异步任务检查()。 2. 从 Main 执行 db.check().GetAwaiter().GetResult();
-
@CraigWilson 超时后失败。这是什么意思?
-
这可能意味着它无法与服务器通信,但我没有任何堆栈跟踪或异常可以肯定地告诉你。
-
@CraigWilson 那个时候 mongod 没有启动,我尝试启动它,程序运行了!我是否需要设置某种服务才能在不手动启动 mongod 的情况下工作?对于这样的问题,我很抱歉(编程、数据库、使用 shell 等)
标签: c# mongodb mongodb-.net-driver