开发者

How to Select All Documents of a Type in RavenDB

开发者 https://www.devze.com 2023-01-18 02:52 出处:网络
So far, I\'ve tried the following: public class Widget { public int Id; public string Name; } public static class Main

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 t开发者_如何学Co 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消