【问题标题】:How can I get all documents from RavenDB?如何从 RavenDB 获取所有文档?
【发布时间】:2013-11-07 08:38:26
【问题描述】:
[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.net 已经关闭了两个小时......

标签: c# ravendb


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 好的,如果我只想要 100 个呢?这里的问题不是我的结果集是有界的,而是这段代码抛出了异常。 session.Query().Take(200).ToList() 抛出同样的异常。
    • 存储库模式的使用使得很难理解幕后发生的事情。您在查询之前是否调用了 SaveChanges?
    猜你喜欢
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    相关资源
    最近更新 更多