【发布时间】:2012-05-11 03:27:32
【问题描述】:
我在 RavenDb 中存储了 2 种不同的对象类型,它们是父/子类型关系,就像 JSON 中的这样:
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;}
}
【问题讨论】: