Question

[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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top