RavenDB Get By List of IDs?

旧巷老猫 提交于 2019-12-13 00:44:11

问题


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:


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.)



来源:https://stackoverflow.com/questions/19746685/ravendb-get-by-list-of-ids

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!