如何从RavenDB获取所有文档?

时间:2013-10-28 23:51:56

标签: c# 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类型的所有文档?

2 个答案:

答案 0 :(得分:2)

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

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());

如果你想要删除一个不同的索引,那么它应该也可以。我们也在使用这种模式进行一些测试。

答案 1 :(得分:1)

由于默认分页RavenDB强制执行,它将无法工作。看看这里:http://ayende.com/blog/161249/ravendbs-querying-streaming-unbounded-results

相关问题