Mongodb FindAll 未按预期工作

发布于 2025-01-02 06:25:52 字数 1742 浏览 0 评论 0原文

[TestMethod]
public void TestLoop()
{
    var server = MongoServer.Create(
        @"mongodb://user:[email protected]:2700/XXX");

    var database = server["XXX"];

    MongoCollection<Item> sourceCollection =database.GetCollection<Item>("Item");
    var counter = 0;
    int batchSize = 200;

    List<item> batch = new List<item>();
    foreach (var item in sourceCollection.FindAll().SetBatchSize(batchSize))
    {
        counter++;
        batch.Add(item);
    }
}

这是一个简单的测试函数,用于检索用于测试目的的集合。它之前工作正常,但它被破坏并抛出以下错误。

无法从传输连接读取数据:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机未能响应。< /em>

当光标想要获取下一批数据时抛出此错误。连接似乎已断开,因此我修改了代码以解决强制重新连接的问题。

[TestMethod]
public void TestLoop()
{
    var server = MongoServer.Create(
        @"mongodb://user:[email protected]:2700/XXX");

    var database = server["XXX"];

    MongoCollection<Item> sourceCollection =database.GetCollection<Item>("Item");
    var counter = 0;
    int batchSize = 200;

    List<item> batch = new List<item>();
    foreach (var item in sourceCollection.FindAll().SetBatchSize(batchSize))
    {
        //serverX.Reconnect();
        counter++;
        if (counter% batchSize == 0)
        {
           server.Reconnect();
        }

        batch.Add(item);
    }
}

我想知道我的原始代码有什么问题。唯一的区别是我的 mongodb 托管 mongolab 刚刚将其版本提升到 2.0.2。任何提示都是值得赞赏的。

[TestMethod]
public void TestLoop()
{
    var server = MongoServer.Create(
        @"mongodb://user:[email protected]:2700/XXX");

    var database = server["XXX"];

    MongoCollection<Item> sourceCollection =database.GetCollection<Item>("Item");
    var counter = 0;
    int batchSize = 200;

    List<item> batch = new List<item>();
    foreach (var item in sourceCollection.FindAll().SetBatchSize(batchSize))
    {
        counter++;
        batch.Add(item);
    }
}

This is a simple test function to retrieve a collection for testing purpose. It work fine before but it is broken and throw the following error.

Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

This error is throw as the cursor want to get the next batch of data. It seems the connection is dropped so I modified my code for a work around to force reconnect.

[TestMethod]
public void TestLoop()
{
    var server = MongoServer.Create(
        @"mongodb://user:[email protected]:2700/XXX");

    var database = server["XXX"];

    MongoCollection<Item> sourceCollection =database.GetCollection<Item>("Item");
    var counter = 0;
    int batchSize = 200;

    List<item> batch = new List<item>();
    foreach (var item in sourceCollection.FindAll().SetBatchSize(batchSize))
    {
        //serverX.Reconnect();
        counter++;
        if (counter% batchSize == 0)
        {
           server.Reconnect();
        }

        batch.Add(item);
    }
}

I want to know what's wrong of my orginal code. The only difference thing is my mongodb hosting mongolab just promoted its version to 2.0.2. Any hints is appreciate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这样的小城市 2025-01-09 06:25:52

已解决。这不是代码或数据库版本的问题。有应用程序在后台运行并消耗网络资源。

关闭该应用程序并重新运行测试后。测试顺利。

Resolved. It is not a matter of code or db version. There are application running in background and consume the network resource.

After closing that application and re-run the test. The test go fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文