如何从 RavenDB 获取所有文档

本文关键字:文档 获取 RavenDB | 更新日期: 2023-09-27 18:37:08

[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 类型的文档?

如何从 RavenDB 获取所有文档

如果你想要的是删除所有内容,那么你可以这样做:

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