【发布时间】:2015-06-18 23:38:33
【问题描述】:
当我尝试从集合中检索数千个文档时,我一直遇到此错误。
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;
当我查看异常消息时,它说“索引超出范围。必须是非负数并且小于集合的大小。参数:索引”
【问题讨论】:
-
collection.Find返回什么? -
我有一个“Book”类,我在初始化 MongoCollection 时使用它。所以 .find 将返回一个“书”列表。而“Book”实现了我的“IProduct”接口
-
PDatabase.GetCollection<Book>("books")retuns 什么?显然products在上下文中无效 -
我编辑了它。它的“书”,每本书都添加到 Iproduct 列表中。
-
如果
books为空,则try不应执行.. :(
标签: c# mongodb mongodb-.net-driver