Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Select All Documents of a Type in RavenDB

Tags:

c#

ravendb

So far, I've tried the following:

public class Widget
{
    public int Id;
    public string Name;
}

public static class Main
{
    public static void Main()
    {
        // Initialize store and preload with widgets...

        using (var session = store.OpenSession())
        {
            var widgets = session.Load<Widget>();
            foreach(var widget in widgets)
            {
                Console.WriteLine(widget.Name);
            }
        }
    }
}

I have been able to load all by adding an index and then using that index as a query:

var store = new DocumentStore();
store.DatabaseCommands.PutIndex("AllWidgets", new IndexDefinition<Widget>
{
    Map = widget => from widget in widgets
                   select new { widget }
});

// Back in Main
var widgets = session.Query<Widget>("AllWidgets");
// Do stuff with widgets.

Is there a way to just get all documents of type Widget without having to create an index?

At this point I'm just playing with RavenDB in a sandbox environment. I realize that this is usually not the best approach to fetching data.

like image 654
Doctor Blue Avatar asked Sep 28 '10 13:09

Doctor Blue


1 Answers

Yes

use the DocumentsByName query - this as far as I can work out is not intuitive in the client interface at the moment, but looks something like this:

documentSession.LuceneQuery<ImageDocument>("Raven/DocumentsByEntityName")
                 .Where("Tag:Widgets")
                 .Take(100)
                 .ToArray();

It helps if you know the HTTP API sometimes :)

NB: Note how it pluralises for you, this is a convention and can be overridden.

Note: In the unstable fork (so likely to be stable soon, the above can easily be achieved with

documentSession.Query<ImageDocument>().Take(100).ToArray()

Much nicer

like image 152
Rob Ashton Avatar answered Oct 14 '22 07:10

Rob Ashton