查询太大的mongodb c#

时间:2017-04-06 14:42:18

标签: c# mongodb

我正在尝试在robomongo中进行查询,但查询中似乎有太多结果。

List<DataObject> list = collection.FindAs<DataObject>(Query.EQ("Item1", "Value1")).ToList();
foreach (DataObject item in list)
{
    //Do Something
}

根据我的阅读,我需要使用MongoCursor,但我找不到一个好的例子,是否使用1000批次迭代所有内容?

类似的东西:

MongoCursor<DataObject> cursor = collection.FindAs<DataObject>(Query.EQ("Item1", "Value1"));
int batchNumber = 1000;
List<DataObject> list;
while(list = cursor.getBatch(batchNumber);)
{
    foreach (DataObject item in list)
    {
        //Do Something
    }
}

现在我知道如果我不在foreach之前将它保存在列表中,我可以很容易地解决这个问题:

foreach (DataObject item in collection.FindAs<DataObject>(Query.EQ("Item1", "Value1")))
{
     //Do Something
}

1 个答案:

答案 0 :(得分:0)

This was solved by not saving the result in a list before the foreach. Like this

foreach (DataObject item in collection.FindAs<DataObject>(Query.EQ("Item1", "Value1")))
{
     //Do Something
}