Account/1{ "name": "Acc1",}Items/1{ "Account": "Account/1","Value" : "100","Tags": [ "tag1","tag2"]}Items/2{ "Account": "Account/1","Value" : "50","Tags": [ "tag2"]}
请注意,我不想将它们存储在同一文档中,因为一个帐户可能有数千个项目.
我正在尝试编写一个map / reduce索引,它会返回类似于:
{ "Account": "Acc1","TagInfo": [ { "Tagname" : "tag1","Count" : "1",//Count of all the "tag1" occurrences for acc1 "Value" : "100" //Sum of all the Values for acc1 which are tagged 'tag1' },{ "Tagname" : "tag2","Count" : "2",//Two items are tagged "tag2" "Value" : "150" }]}
即所有不同标签名称的列表,以及每个标签名称的数量及其值.
我想我需要使用多地图将Account和Items集合映射到一起,但我无法找出reduce部分来创建结果的“TagInfo”部分.
这是可能的,还是我在Raven模拟这个错误?
编辑:
我想从这个查询中检索的类看起来像这样:
public class queryResult{ public string AccountID {get;set;} public TagInfo Tags {get;set;} }public class TagInfo{ public string Tagname {get;set;} public int Count {get;set;} public int TotalSum {get;set;}}解决方法 您不能使用Multi Map / Reduce索引,因为您需要在标签上放置一个地图而在帐户上放置另一个地图.他们没有共同的属性,所以你不能在这里有多个地图/减少.
但是,您可以使用transformResult.这是怎么做的:
public class Account{ public string ID { get; set; } public string name { get; set; }}public class Item{ public string ID { get; set; } public string AccountID { get; set; } public int Value { get; set; } public List<string> Tags { get; set; }}public class TagsWithCountAndValues : AbstractIndexCreationTask<Item,TagsWithCountAndValues.ReduceResult>{ public class ReduceResult { public string AccountID { get; set; } public string Accountname { get; set; } public string Tag { get; set; } public int Count { get; set; } public int TotalSum { get; set; } } public TagsWithCountAndValues() { Map = items => from item in items from tag in item.Tags select new { AccountID = item.AccountID,Tag = tag,Count = 1,TotalSum = item.Value }; Reduce = results => from result in results group result by result.Tag into g select new { AccountID = g.Select(x => x.AccountID).FirstOrDefault(),Tag = g.Key,Count = g.Sum(x => x.Count),TotalSum = g.Sum(x => x.TotalSum) }; transformResults = (database,results) => from result in results let account = database.Load<Account>(result.AccountID) select new { AccountID = result.AccountID,Accountname = account.name,Tag = result.Tag,Count = result.Count,TotalSum = result.TotalSum }; }}
之后,您可以像这样查询:
var results = session.query<TagsWithCountAndValues.ReduceResult,TagsWithCountAndValues>() .Where(x => x.AccountID == "accounts/1") .ToList();总结
以上是内存溢出为你收集整理的c# – 使用子集合在RavenDb中映射2个集合全部内容,希望文章能够帮你解决c# – 使用子集合在RavenDb中映射2个集合所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)