[英]How can I get all documents from RavenDB?
[Test]
public void Can_Get_All()
{
var repository = new RavenRepository<Motorcycle>();
repository.DeleteAll();
repository.Store(new Motorcycle {Make = "Datsun", YearManufactured = 1972});
repository.Store(new Motorcycle {Make = "Toyota", YearManufactured = 2002});
IList<Motorcycle> savedThings = repository.GetAll();
Assert.IsTrue(savedThings.Count == 2);
}
RavenRepository.GetAll()
public IList<T> GetAll()
{
using (IDocumentSession session = _collection.OpenSession())
{
return session.Query<T>().ToList(); // Throws exception
}
}
運行此測試會引發異常:
Raven.Abstractions.Exceptions.IndexCompilationException:無法理解查詢:變量初始化程序選擇必須具有帶對象創建表達式的lambda表達式
為什么? 如何從RavenDB中獲取T類型的所有文檔?
如果您想要刪除所有內容,那么您可以這樣做:
public class AllDocumentsById : AbstractIndexCreationTask
{
public override IndexDefinition CreateIndexDefinition()
{
return
new IndexDefinition
{
Name = "AllDocumentsById",
Map = "from doc in docs
let DocId = doc[\"@metadata\"][\"@id\"]
select new {DocId};"
};
}
}
docStore.DatabaseCommands.DeleteByIndex("AllDocumentsById", new IndexQuery());
如果你有一個你想刪除的索引,那么它應該也可以。 我們也在使用這種模式進行一些測試。
由於默認分頁RavenDB強制執行,它將無法工作。 看看這里: http : //ayende.com/blog/161249/ravendbs-querying-streaming-unbounded-results
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.