emilio
emilio

Reputation: 314

Querying RavenDB

I am able to insert and delete data in a RavenDB store. I am also able to run queries that return multiple documents.

I am however unable to load data using session.Load<Book>("books/0") which returns null. Additionally, I get an InvalidOperationException when I try and run a query that searches for a book with a given ID but this particular behavior is expected.

Is there anything I am missing when fetching the doc?

Code Block:

using (var session = DocumentStoreHolder.Store.OpenSession())
{
    for (int i = 0; i < count; i++)
    {
        var doc = session.Load<Book>(i); 
        Console.WriteLine(doc);
    }
}

Upvotes: 0

Views: 383

Answers (2)

Stephen Brickner
Stephen Brickner

Reputation: 2602

It's been a little while since I used Raven but I believe you need to use a Query not Load.

using (var session = DocumentStoreHolder.Store.OpenSession())
{
    var books = session.Query<Book>().ToList();
    books.ForEach(book =>
    {
         //Note that book is an object, write out one of it's properties
         Console.WriteLine(book.Name); 
    });

    //or get single book:
    var id = 1;
    var book = session.Query<Book>()
       .FirstOrDefault(x => x.ID == id); 
}

Upvotes: 1

Ayende Rahien
Ayende Rahien

Reputation: 22956

Do you have a document with the id books/0? Typically document ids start with 1, so you'll have books/1, etc.

Upvotes: 3

Related Questions