【问题标题】:Couchbase View _count Reduce For Given KeysCouchbase View _count 减少给定键
【发布时间】:2015-03-31 21:38:55
【问题描述】:

我正在尝试使用诸如 _count 之类的 reduce 在 Couchbase 中编写视图,这将给我一个地址的产品计数。

我在数据库中有一些文件格式如下;

文档 1

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

现在这将我引向实际问题,我希望能够获得一个或多个地址的产品计数。

我希望能够查看“键”数组

['W Churchill St','Chicago','IL']
['E Foster Rd','New York','NY']

哪些产品和数量。所以我希望看到我的结果。

'Cable' : 2,
'Satellite': 1,
'Free To Air': 1

但是,如果我只指定了这个“键”,

['W Churchill St','Chicago','IL']

我希望看到

'Cable' : 2,
'Satellite': 1

如何编写我的视图来适应这种情况?

【问题讨论】:

    标签: mapreduce couchbase reduce


    【解决方案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}
    ]}
    

    然后我需要对每个地址重复此查询并对结果求和。

    【讨论】:

      猜你喜欢
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多