Mapreduce Couchbase视图\减少给定密钥的计数
我试图在Couchbase中使用一个reduce(如_count)来编写一个视图,它将为我提供一个地址处的产品计数 我在数据库中有以下格式的文件 文件1Mapreduce Couchbase视图\减少给定密钥的计数,mapreduce,couchbase,reduce,Mapreduce,Couchbase,Reduce,我试图在Couchbase中使用一个reduce(如_count)来编写一个视图,它将为我提供一个地址处的产品计数 我在数据库中有以下格式的文件 文件1 { id: 1, address: { street: 'W Churchill St' city: 'Chicago', state: 'IL', }, product: 'Cable' } 文件2 { id: 2, address: {
{
id: 1,
address: {
street: 'W Churchill St'
city: 'Chicago',
state: 'IL',
},
product: 'Cable'
}
文件2
{
id: 2,
address: {
street: 'W Churchill St'
city: 'Chicago',
state: 'IL',
},
product: 'Cable'
}
文件3
{
id: 3,
address: {
street: 'W Churchill St'
city: 'Chicago',
state: 'IL',
},
product: 'Satellite'
}
文件4
{
id: 4,
address: {
street: 'E Foster Rd'
city: 'New York',
state: 'NY',
},
product: 'Free To Air'
}
我已经有了一个视图,它为我提供了一个使用复合密钥的地址上的所有产品,例如
emit([doc.address.street, doc.address.city, doc.address.state], null)
现在,这就引出了实际问题,我希望能够在一个或多个地址获得产品数量
我希望能够看到一组“键”
哪些产品以及它们的数量。所以我希望能看到我的结果
'Cable' : 2,
'Satellite': 1,
'Free To Air': 1
但是如果我只指定了这个“密钥”
我希望看到
'Cable' : 2,
'Satellite': 1
如何编写我的视图以适应这种情况?解决方案是将我的产品添加到密钥中,就像这样
emit([doc.address.street, doc.address.city, doc.address.state, doc.product], null)
然后使用
?start_key=[street,city,state]&end_key=[street,city,state,{}]&group_level=4
结果:
{"rows":[
{"key":['W Churchill St','Chicago','IL','Cable'], "value":2},
{"key":['W Churchill St','Chicago','IL','Satellite'], "value":1}
]}
然后,我需要对每个地址重复这个查询,并对结果求和
?start_key=[street,city,state]&end_key=[street,city,state,{}]&group_level=4
{"rows":[
{"key":['W Churchill St','Chicago','IL','Cable'], "value":2},
{"key":['W Churchill St','Chicago','IL','Satellite'], "value":1}
]}