我一直有这个错误,因为我试图从集合中检索数千个文档。MongoDB。索引超出范围
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'返回什么? –
我有一个'书'类,我用它来初始化MongoCollection。所以.find会返回一个'Book'列表。 'Book'实现我的'IProduct'接口 –
'PDatabase.GetCollection(“books”)'回调什么?显然'products'在上下文中无效 –