MongoDB。索引超出范围

MongoDB. Index was out of range

当我尝试从集合中检索数千个文档时,我一直遇到此错误。

index was out of range. must be non-negative and less than the size of the collection. parameter : Index

这是我查询文档时的代码:

        MongoCollection<Book> collection = PDatabase.GetCollection<Book>("books");
        List<IProduct> products = new List<IProduct>();
        var books = collection.Find(Query.GT("stock", 0)).SetFlags(QueryFlags.NoCursorTimeout);
        foreach (var book in books)
        {
            try
            {
                products.Add(book);
            }
            catch (Exception ex) { Console.WriteLine(ex.Message);  }
        }
        return products;

当我查看异常消息时,它显示 "index was out of range. must be non-negative and less than the size of the collection. Parameter : Index"

您在评论中提到根本问题是 OutOfMemoryException。

此处的回答建议分页最佳做法MongoDB - paging