1
[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
    }
}

Running this test throws an exception:

Raven.Abstractions.Exceptions.IndexCompilationException : Could not understand query: Variable initializer select must have a lambda expression with an object create expression

Why? How can I just get all the documents of type T out of RavenDB?

Daniel Coffman
  • 1,997
  • 3
  • 26
  • 34

2 Answers2

2

If what you want is to delete everything, then you can do this:

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

If you have a different index that you want to delete based on then that should work as well. We are using this pattern for some tests as well.

Pez
  • 178
  • 1
  • 6
1

It won't work because of default paging RavenDB enforces. Take a look here: http://ayende.com/blog/161249/ravendbs-querying-streaming-unbounded-results

synhershko
  • 4,472
  • 1
  • 30
  • 37
  • Okay, what if I want just 100 of them? The problem here isn't that my result set is bounded, it's that this code throws an exception. session.Query().Take(200).ToList() throws the same exception. – Daniel Coffman Oct 29 '13 at 18:44
  • The use of the repository pattern makes it hard to understand whats going on under the hood. Are you calling SaveChanges before you query? – synhershko Oct 29 '13 at 21:29