1

In RavenDB

I need to get the latest insert for a document based on it's ID and filter by IDs from a list

ie:

List<Entity> GetLastByIds(List<int> ids);

The Entity is similar to:

class Entity
{
int id; //Unique identifier for the category the price applies to.
int Price; //Changes with time
DateTime Timestamp; //DateTime.UtcNow
}

so if I insert the following:

session.Store(new Entity{Id = 1, Price = 12.5});
session.Store(new Entity{Id = 1, Price = 7.2});
session.Store(new Entity{Id = 1, Price = 10.3});
session.Store(new Entity{Id = 2, Price = 50});
session.Store(new Entity{Id = 3, Price = 34});
...

How can I get the latest price for IDs 1 and 3 ??

I have the Map/Reduce working fine, giving me the latest for each ID, it's the filtering that I am struggling with. I'd like to do the filtering in Raven, because if there are over 1024 price-points in total for all IDs, doing filtering on the client side is useless.

Much appreciate any help I can get.

Thank you very much in advance :)

1 Answers1

5

If the Id is supposed to represent the category, then you should call it CategoryId. By calling the property Id, you are picking up on Raven's convention that it should be treated as the primary key for that document. You can't save multiple versions of the same document. It will just overwrite the last version.

Assuming you've built your index correctly, you would just query it like so:

using Raven.Client.Linq;

...

var categoryIds = new[] {1, 3};  // whatever
var results = session.Query<Entity, YourIndex>()
                     .Where(x=> x.CategoryId.In(categoryIds));

(The .In extension method is in the Raven.Client.Linq namespace.)

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • That worked like a charm, but it seems that I definitely need help with my indexes now. –  Nov 05 '13 at 00:43