CouchDB 视图 - 在键数组上过滤和分组 [英] CouchDB View - Filter and Group By on Key Array

查看:33
本文介绍了CouchDB 视图 - 在键数组上过滤和分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题描述

我在 CouchDB 视图中有一组键,[doc.time, doc.address].两者都不是独一无二的.doc.time 是一个 UNIX 时间戳,doc.address 是一个字符串.reduce 函数设置为 _sum,因为每组键的唯一值是一个数字.

我想要的是按doc.time过滤,然后按doc.address对剩余的记录进行分组.如果我将 doc.time 作为第一个键,无论我指定什么作为 group_level,我似乎都无法按唯一地址进行分组.如果我把 doc.address 放在第一位,我似乎无法按时间过滤查询.

两个例子

查询:?group_level=1&startkey=[0,1230000000]&endkey=[{},1340000000]

First Key: doc.address 之前的 doc.time

问题:没有按时间过滤

代码:

行:[{密钥:[1126GDuGLQTX3LFHHmjCctdn8WKDjn7QNA"],价值:50},{密钥:[112AobLhjLJQ3LGqXFrsdnWMPqWCQqoiS6"],价值:50}]

<小时>

查询:?group_level=1&startkey=[1230000000]&endkey=[1340000000,{}]

First Key: doc.timedoc.address

之前

问题:看不到而且我没有按doc.address

分组

代码:

行:[{密钥:[ 1231469665 ],价值:50},{密钥:[ 1231469744 ],价值:50}]

解决方案

您提到:

<块引用>

...如果我将 doc.time 作为第一个键,无论我指定为 group_level 什么,我似乎都无法按唯一地址进行分组...

查询参数 group_level=NNth 逗号上拆分字符串,并通过字符串匹配将左侧元素组合在一起.因此,当你的数组键是这样的:[doc.time, doc.address],你将无法按address分组>,不在逗号的左边.

<块引用>

...如果我把 doc.address 放在第一位,我似乎无法按时间过滤查询...

当你的数组键是这样的:[doc.address, doc.time],注意你在里面发出一个数组键你的地图功能.关于 CouchDB 中的数组键复合键,您需要考虑以下几点:

<小时>

此参考:

<块引用>

...首先要注意和非常重要的...一个数组输出...来自javascript Map函数...每个索引键都是字符串,并按字符排序字符作为字符串,包括括号和逗号...

以上关于参考对 CouchDB 索引的工作方式有重大影响.

为了澄清,让我们在 sample 数据库上创建如下文档:

{"time":"2011","address":"CT"}{"time":"2012","address":"CT"}...{时间":2011",地址":TX"}...{"time":"2015","address":"TX"}...{时间":2014",地址":纽约"}...{"time":"2014","address":"CA"}{"time":"2015","address":"CA"}{"time":"2016","address":"CA"}

我实现了一个这样的视图映射函数:

function (doc) {if(doc.time && doc.address){发射([文档地址,文档时间],空);}}

目前,我没有使用任何 Reduce 函数,因为,让我们忽略任何 分组reducing 并专注于简单的 <强>索引.上面的视图正在生成以下用于索引的键/值对:

$ curl -k -X GET 'https://admin:****@192.168.1.106:6984/sample/_design/by_addr_time/_view/by_addr_time'{"total_rows":25,"offset":0,"rows":[{"id":"doc_0022","key":["CA","2014"],"value":null},{"id":"doc_0023","key":["CA","2015"],"value":null},{"id":"doc_0024","key":["CA","2016"],"value":null},{"id":"doc_0000","key":["CT","2011"],"value":null},{"id":"doc_0001","key":["CT","2012"],"value":null},{"id":"doc_0002","key":["CT","2013"],"value":null},{"id":"doc_0003","key":["CT","2014"],"value":null},{"id":"doc_0004","key":["CT","2015"],"value":null},{"id":"doc_0005","key":["CT","2016"],"value":null},{"id":"doc_0014","key":["NY","2011"],"value":null},{"id":"doc_0015","key":["NY","2012"],"value":null},{"id":"doc_0016","key":["NY","2013"],"value":null},{"id":"doc_0017","key":["NY","2014"],"value":null},{"id":"doc_0018","key":["NY","2015"],"value":null},{"id":"doc_0019","key":["NY","2016"],"value":null},{"id":"doc_0020","key":["NY","2017"],"value":null},{"id":"doc_0021","key":["NY","2018"],"value":null},{"id":"doc_0006","key":["TX","2011"],"value":null},{"id":"doc_0008","key":["TX","2012"],"value":null},{"id":"doc_0007","key":["TX","2013"],"value":null},{"id":"doc_0009","key":["TX","2014"],"value":null},{"id":"doc_0010","key":["TX","2015"],"value":null},{"id":"doc_0011","key":["TX","2016"],"value":null},{"id":"doc_0012","key":["TX","2017"],"value":null},{"id":"doc_0013","key":["TX","2018"],"value":null}]}

现在,我将执行查询以按 doc.time 过滤视图.我的查询参数是:

?startkey=["AA","2017"]&endkey=["ZZ","2018"]

我希望上述查询仅返回 20172018 之间带有 time 字段的文档,address 字段可以具有 any 值,因为我指定了从 AAZZ 的值,其中包括我数据库中的所有地址.我正在使用 curl 进行查询,如下所示:

$ curl -k -X GET 'https://admin:****@192.168.1.106:6984/sample/_design/by_addr_time/_view/by_addr_time?startkey=["AA","2017"]&endkey=["ZZ","2018"]'{"total_rows":25,"offset":0,"rows":[{"id":"doc_0022","key":["CA","2014"],"value":null},{"id":"doc_0023","key":["CA","2015"],"value":null},{"id":"doc_0024","key":["CA","2016"],"value":null},{"id":"doc_0000","key":["CT","2011"],"value":null},{"id":"doc_0001","key":["CT","2012"],"value":null},{"id":"doc_0002","key":["CT","2013"],"value":null},{"id":"doc_0003","key":["CT","2014"],"value":null},{"id":"doc_0004","key":["CT","2015"],"value":null},{"id":"doc_0005","key":["CT","2016"],"value":null},{"id":"doc_0014","key":["NY","2011"],"value":null},{"id":"doc_0015","key":["NY","2012"],"value":null},{"id":"doc_0016","key":["NY","2013"],"value":null},{"id":"doc_0017","key":["NY","2014"],"value":null},{"id":"doc_0018","key":["NY","2015"],"value":null},{"id":"doc_0019","key":["NY","2016"],"value":null},{"id":"doc_0020","key":["NY","2017"],"value":null},{"id":"doc_0021","key":["NY","2018"],"value":null},{"id":"doc_0006","key":["TX","2011"],"value":null},{"id":"doc_0008","key":["TX","2012"],"value":null},{"id":"doc_0007","key":["TX","2013"],"value":null},{"id":"doc_0009","key":["TX","2014"],"value":null},{"id":"doc_0010","key":["TX","2015"],"value":null},{"id":"doc_0011","key":["TX","2016"],"value":null},{"id":"doc_0012","key":["TX","2017"],"value":null},{"id":"doc_0013","key":["TX","2018"],"value":null}]}

上述查询返回的响应看起来令人震惊.因为它看起来不只返回 20172018 之间提交的 time 文档.数组键的CouchDB索引就是这样工作的.CouchDB 对 数组键 进行索引,就好像整个数组是一个字符串,包括数组的括号和逗号! 如果您阅读 参考,它会开始制作感觉.

现在让我们更改查询:

?startkey=["CT","2016"]&endkey=["TX","2011"]

以上查询的结果如下所示,根据我们的解释,这应该是有道理的:

$ curl -k -X GET 'https://admin:****@192.168.1.106:6984/sample/_design/by_addr_time/_view/by_addr_time?startkey=["CT","2016"]&endkey=["TX","2011"]'{"total_rows":25,"offset":8,"rows":[{"id":"doc_0005","key":["CT","2016"],"value":null},{"id":"doc_0014","key":["NY","2011"],"value":null},{"id":"doc_0015","key":["NY","2012"],"value":null},{"id":"doc_0016","key":["NY","2013"],"value":null},{"id":"doc_0017","key":["NY","2014"],"value":null},{"id":"doc_0018","key":["NY","2015"],"value":null},{"id":"doc_0019","key":["NY","2016"],"value":null},{"id":"doc_0020","key":["NY","2017"],"value":null},{"id":"doc_0021","key":["NY","2018"],"value":null},{"id":"doc_0006","key":["TX","2011"],"value":null}]}

<小时>

更新

<块引用>

...我想要的是按doc.time过滤,然后按doc.address对剩余记录进行分组...

那么,我们该怎么办?有一个很好的问题和答案,并提供了基本思路.

不确定哪个想法是最好的,但我实现了一个这样的想法:创建一个名为 t_red 的视图,如下所示,带有一个内置的 _count 减少:

function (doc) {if(doc.time && doc.address){发射([文档时间,文档地址],空);}}

此外,我创建了一个名为 a_red 的视图,其中包含一个内置的 _count reduce:

function (doc) {if(doc.address && doc.time){发射([文档地址,文档时间],空);}}

然后我在 NodeJS 上开发了以下代码来查询 20122015 之间的 doc.time 然后根据doc.address对结果进行分组,控制台日志在代码中显示为注释.我希望这段代码会有所帮助(不要混淆!):

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";//忽略拒绝,因为 CouchDB SSL 证书是自签名的const fetch=require('node-fetch')//查询t_red"视图/索引fetch(`https://admin:****@192.168.1.106:6984/sample/_design/t_red/_view/t_red?group_level=2&startkey=["2012", "AA"]&endkey=["2015", "ZZ"]`, {方法:'获取',标题:{'内容类型':'应用程序/json',}}).然后(res=>res.json()).then(数据=>{让 unique_addr=[]data.rows.map(row=>{console.log('row.key->', row.key, 'row.value->', row.value)//控制台日志如下所示:////row.key->['2012','CT'] row.value->1//row.key->['2012', 'NY'] row.value->1//row.key->['2012', 'TX'] row.value->1//row.key->['2013','CT'] row.value->1//row.key->['2013', 'NY'] row.value->1//row.key->['2013', 'TX'] row.value->1//row.key->['2014','CA'] row.value->1//row.key->['2014','CT'] row.value->1//row.key->['2014', 'NY'] row.value->1//row.key->['2014', 'TX'] row.value->1//row.key->['2015','CA'] row.value->1//row.key->['2015','CT'] row.value->1//row.key->['2015', 'NY'] row.value->1//row.key->['2015', 'TX'] row.value->1if(unique_addr.indexOf(row.key[1])==-1){//将唯一地址推入数组unique_addr.push(row.key[1])}})console.log(unique_addr)//控制台日志如下所示:////[ 'CT', 'NY', 'TX', 'CA' ]返回唯一地址}).then(unique_addr=>{//对唯一地址进行分组让 group_by_address=unique_addr.map(addr=>{//对于每个唯一的地址,查询a_red"视图/索引return fetch(`https://admin:****@192.168.1.106:6984/sample/_design/a_red/_view/a_red?group_level=2&startkey=["${addr}","2012"]&;endkey=["${addr}","2015"]`, {方法:'获取',标题:{'内容类型':'应用程序/json',}}).然后(res=>res.json()).then(数据=>{data.rows.map(row=>{console.log('row.key->',row.key,'row.value->',row.value)})//与这部分代码相关的控制台日志如下所示//row.key->['CA','2014'] row.value->1//row.key->['CA','2015'] row.value->1//row.key->['纽约','2012'] row.value->1//row.key->['纽约','2013'] row.value->1//row.key->['纽约','2014'] row.value->1//row.key->['纽约','2015'] row.value->1//row.key->['CT','2012'] row.value->1//row.key->['CT','2013'] row.value->1//row.key->['CT','2014'] row.value->1//row.key->['CT', '2015'] row.value->1//row.key->['TX', '2012'] row.value->1//row.key->['TX', '2013'] row.value->1//row.key->['TX', '2014'] row.value->1//row.key->['TX', '2015'] row.value->1让对象={}obj[addr]=data.rows.length//这个对象在上面的查询中包含唯一的地址和对应的频率返回对象}).catch(err=>{console.log('err->', err)})})返回 group_by_address}).then(group_by_address=>{group_by_address.map(group=>{group.then(()=>{console.log('按地址分组->', group)//这段代码相关的控制台日志如下所示://按地址分组->承诺 { { CA: 2 } }//按地址分组->承诺 { { 纽约:4 } }//按地址分组->承诺 { { CT: 4 } }//按地址分组->承诺 { { TX: 4 } }})})}).catch(err=>{console.log('err->', err)})

Description of Problem

I have an array of keys in a CouchDB view, [doc.time, doc.address]. Neither is unique. doc.time is a UNIX timestamp and doc.address is a string. The reduce function is set to _sum as the only value for each set of keys is a number.

What I want is to filter by doc.time, then group the remaining records by doc.address. If I put doc.time as the first key, I cannot seem to group by unique addresses no matter what I specify as a group_level. If I put doc.address first, I cannot seem to filter the query by time.

Two Examples

Query: ?group_level=1&startkey=[0,1230000000]&endkey=[{},1340000000]

First Key: doc.address before doc.time

Problem: Does not filter by time

Code:

rows: [
  {
    key: [ "1126GDuGLQTX3LFHHmjCctdn8WKDjn7QNA" ],
    value: 50
  },
  {
    key: [ "112AobLhjLJQ3LGqXFrsdnWMPqWCQqoiS6" ],
    value: 50
  }
]


Query: ?group_level=1&startkey=[1230000000]&endkey=[1340000000,{}]

First Key: doc.time before doc.address

Problem: Cannot see and I am not grouped by doc.address

Code:

rows: [
  {
    key: [ 1231469665 ],
    value: 50
  },
  {
    key: [ 1231469744 ],
    value: 50
  }
]

解决方案

You mentioned that:

... If I put doc.time as the first key, I cannot seem to group by unique addresses no matter what I specify as a group_level ...

The query parameter group_level=N splits the string on the Nth comma and groups the left elements together by string match. Therefore, When your array key is like this: [doc.time, doc.address], you won't be able to group by address, which is not on the left side of the comma.

... If I put doc.address first, I cannot seem to filter the query by time ...

When your array key is like: [doc.address, doc.time], notice that you are emitting an array key inside your Map function. You need to consider the following points regarding array key or compound key in CouchDB:


Described on this reference:

... First thing of note and very important ... an array output ... from the javascript Map function ... each of those Index Keys are strings, and are ordered character by character as strings, including the brackets and commas ...

The above statement and explanations on the reference have a significant impact on how CouchDB indexing works in the case of compound key or array key.

To clarify, lets create documents like below on a sample database:

{"time":"2011","address":"CT"}
{"time":"2012","address":"CT"}
...
{"time":"2011","address":"TX"}
...
{"time":"2015","address":"TX"}
...
{"time":"2014","address":"NY"}
...
{"time":"2014","address":"CA"}
{"time":"2015","address":"CA"}
{"time":"2016","address":"CA"}

I implemented a view map function like this:

function (doc) {
  if(doc.time && doc.address){
    emit([doc.address, doc.time], null);
  }
}

For now, I'm not using any Reduce function, because, lets ignore any grouping or reducing and focus on plain simple indexing. The above view is generating the following key/value pairs for indexing:

$ curl -k -X GET 'https://admin:****@192.168.1.106:6984/sample/_design/by_addr_time/_view/by_addr_time'
{"total_rows":25,"offset":0,"rows":[
{"id":"doc_0022","key":["CA","2014"],"value":null},
{"id":"doc_0023","key":["CA","2015"],"value":null},
{"id":"doc_0024","key":["CA","2016"],"value":null},
{"id":"doc_0000","key":["CT","2011"],"value":null},
{"id":"doc_0001","key":["CT","2012"],"value":null},
{"id":"doc_0002","key":["CT","2013"],"value":null},
{"id":"doc_0003","key":["CT","2014"],"value":null},
{"id":"doc_0004","key":["CT","2015"],"value":null},
{"id":"doc_0005","key":["CT","2016"],"value":null},
{"id":"doc_0014","key":["NY","2011"],"value":null},
{"id":"doc_0015","key":["NY","2012"],"value":null},
{"id":"doc_0016","key":["NY","2013"],"value":null},
{"id":"doc_0017","key":["NY","2014"],"value":null},
{"id":"doc_0018","key":["NY","2015"],"value":null},
{"id":"doc_0019","key":["NY","2016"],"value":null},
{"id":"doc_0020","key":["NY","2017"],"value":null},
{"id":"doc_0021","key":["NY","2018"],"value":null},
{"id":"doc_0006","key":["TX","2011"],"value":null},
{"id":"doc_0008","key":["TX","2012"],"value":null},
{"id":"doc_0007","key":["TX","2013"],"value":null},
{"id":"doc_0009","key":["TX","2014"],"value":null},
{"id":"doc_0010","key":["TX","2015"],"value":null},
{"id":"doc_0011","key":["TX","2016"],"value":null},
{"id":"doc_0012","key":["TX","2017"],"value":null},
{"id":"doc_0013","key":["TX","2018"],"value":null}
]}

Now, I'm going to do a query to filter the view by doc.time. My query parameters are:

?startkey=["AA","2017"]&endkey=["ZZ","2018"]

I expect the above query to return only the docs with the time field between 2017 and 2018, the address field of those docs can have any value since I specified from AA to ZZ which includes all addresses on my database. I'm doing the query with curl like this:

$ curl -k -X GET 'https://admin:****@192.168.1.106:6984/sample/_design/by_addr_time/_view/by_addr_time?startkey=["AA","2017"]&endkey=["ZZ","2018"]'
{"total_rows":25,"offset":0,"rows":[
{"id":"doc_0022","key":["CA","2014"],"value":null},
{"id":"doc_0023","key":["CA","2015"],"value":null},
{"id":"doc_0024","key":["CA","2016"],"value":null},
{"id":"doc_0000","key":["CT","2011"],"value":null},
{"id":"doc_0001","key":["CT","2012"],"value":null},
{"id":"doc_0002","key":["CT","2013"],"value":null},
{"id":"doc_0003","key":["CT","2014"],"value":null},
{"id":"doc_0004","key":["CT","2015"],"value":null},
{"id":"doc_0005","key":["CT","2016"],"value":null},
{"id":"doc_0014","key":["NY","2011"],"value":null},
{"id":"doc_0015","key":["NY","2012"],"value":null},
{"id":"doc_0016","key":["NY","2013"],"value":null},
{"id":"doc_0017","key":["NY","2014"],"value":null},
{"id":"doc_0018","key":["NY","2015"],"value":null},
{"id":"doc_0019","key":["NY","2016"],"value":null},
{"id":"doc_0020","key":["NY","2017"],"value":null},
{"id":"doc_0021","key":["NY","2018"],"value":null},
{"id":"doc_0006","key":["TX","2011"],"value":null},
{"id":"doc_0008","key":["TX","2012"],"value":null},
{"id":"doc_0007","key":["TX","2013"],"value":null},
{"id":"doc_0009","key":["TX","2014"],"value":null},
{"id":"doc_0010","key":["TX","2015"],"value":null},
{"id":"doc_0011","key":["TX","2016"],"value":null},
{"id":"doc_0012","key":["TX","2017"],"value":null},
{"id":"doc_0013","key":["TX","2018"],"value":null}
]}

The response returned by the above query seems shocking. Because it looks like it did NOT return only the docs with time filed between 2017 and 2018. That's just how the CouchDB indexing for array keys work. CouchDB does the indexing of array keys as if the whole array is a string including the brackets and commas of the array! If you read the reference, it would start to make sense.

Now lets change the query:

?startkey=["CT","2016"]&endkey=["TX","2011"]

The result of the above query is shown below, based on our explanations, this should make sense:

$ curl -k -X GET 'https://admin:****@192.168.1.106:6984/sample/_design/by_addr_time/_view/by_addr_time?startkey=["CT","2016"]&endkey=["TX","2011"]'
{"total_rows":25,"offset":8,"rows":[
{"id":"doc_0005","key":["CT","2016"],"value":null},
{"id":"doc_0014","key":["NY","2011"],"value":null},
{"id":"doc_0015","key":["NY","2012"],"value":null},
{"id":"doc_0016","key":["NY","2013"],"value":null},
{"id":"doc_0017","key":["NY","2014"],"value":null},
{"id":"doc_0018","key":["NY","2015"],"value":null},
{"id":"doc_0019","key":["NY","2016"],"value":null},
{"id":"doc_0020","key":["NY","2017"],"value":null},
{"id":"doc_0021","key":["NY","2018"],"value":null},
{"id":"doc_0006","key":["TX","2011"],"value":null}
]}


UPDATE

... What I want is to filter by doc.time, then group the remaining records by doc.address ...

So, what should we do? There is a good question and answer and provides the basic ideas.

I'm not sure which idea is the best, but I implemented one idea like this: created a view named t_red like below with a builtin _count reduce:

function (doc) {
  if(doc.time && doc.address){
    emit([doc.time, doc.address], null);
  }
}

Also, I created a view named a_red with a builtin _count reduce:

function (doc) {
  if(doc.address && doc.time){
    emit([doc.address, doc.time], null);
  }
}

Then I developed the following code on NodeJS to query doc.time between 2012 and 2015 and then group the results according to the doc.address, console logs are shown inside the code as comments. I hope this code will be helpful (not confusing!):

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; // Ignore rejection, becasue CouchDB SSL certificate is self-signed

const fetch=require('node-fetch')

// query "t_red" view/index
fetch(`https://admin:****@192.168.1.106:6984/sample/_design/t_red/_view/t_red?group_level=2&startkey=["2012", "AA"]&endkey=["2015", "ZZ"]`, {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json',
    }
}).then(
    res=>res.json()
).then(data=>{
    let unique_addr=[]
    data.rows.map(row=>{
        console.log('row.key-> ', row.key, '  row.value-> ', row.value)

        // console log is shown below:
        //
        // row.key->  [ '2012', 'CT' ]   row.value->  1
        // row.key->  [ '2012', 'NY' ]   row.value->  1
        // row.key->  [ '2012', 'TX' ]   row.value->  1
        // row.key->  [ '2013', 'CT' ]   row.value->  1
        // row.key->  [ '2013', 'NY' ]   row.value->  1
        // row.key->  [ '2013', 'TX' ]   row.value->  1
        // row.key->  [ '2014', 'CA' ]   row.value->  1
        // row.key->  [ '2014', 'CT' ]   row.value->  1
        // row.key->  [ '2014', 'NY' ]   row.value->  1
        // row.key->  [ '2014', 'TX' ]   row.value->  1
        // row.key->  [ '2015', 'CA' ]   row.value->  1
        // row.key->  [ '2015', 'CT' ]   row.value->  1
        // row.key->  [ '2015', 'NY' ]   row.value->  1
        // row.key->  [ '2015', 'TX' ]   row.value->  1

        if(unique_addr.indexOf(row.key[1])==-1){ // Push unique addresses into an array
            unique_addr.push(row.key[1])
        }
    })

    console.log(unique_addr)

    // Console log is shown below:
    //
    // [ 'CT', 'NY', 'TX', 'CA' ]

    return unique_addr

}).then(unique_addr=>{

    // Group the unique addresses
    let group_by_address=unique_addr.map(addr=>{
        // For each unique address, do a query of "a_red" view/index
        return fetch(`https://admin:****@192.168.1.106:6984/sample/_design/a_red/_view/a_red?group_level=2&startkey=["${addr}","2012"]&endkey=["${addr}","2015"]`, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            }
        }).then(
            res=>res.json()
        ).then(data=>{
            data.rows.map(row=>{console.log('row.key-> ', row.key, '  row.value-> ', row.value)})

            // Console logs related to this section of code are shown below

            //row.key->  [ 'CA', '2014' ]   row.value->  1
            //row.key->  [ 'CA', '2015' ]   row.value->  1

            //row.key->  [ 'NY', '2012' ]   row.value->  1
            //row.key->  [ 'NY', '2013' ]   row.value->  1
            //row.key->  [ 'NY', '2014' ]   row.value->  1
            //row.key->  [ 'NY', '2015' ]   row.value->  1

            //row.key->  [ 'CT', '2012' ]   row.value->  1
            //row.key->  [ 'CT', '2013' ]   row.value->  1
            //row.key->  [ 'CT', '2014' ]   row.value->  1
            //row.key->  [ 'CT', '2015' ]   row.value->  1

            //row.key->  [ 'TX', '2012' ]   row.value->  1
            //row.key->  [ 'TX', '2013' ]   row.value->  1
            //row.key->  [ 'TX', '2014' ]   row.value->  1
            //row.key->  [ 'TX', '2015' ]   row.value->  1

            let obj={}
            obj[addr]=data.rows.length // This object contains unique address and its corresponding frequency in above query
            return obj

        }).catch(err=>{
            console.log('err-> ', err)
        })
    })

    return group_by_address

}).then(group_by_address=>{
    group_by_address.map(group=>{
        group.then(()=>{
            console.log('Grouped by address-> ', group)

            // Console logs related this section of code are shown below:

            //Grouped by address->  Promise { { CA: 2 } }

            //Grouped by address->  Promise { { NY: 4 } }

            //Grouped by address->  Promise { { CT: 4 } }

            //Grouped by address->  Promise { { TX: 4 } }
        })
    })
}).catch(err=>{
    console.log('err-> ', err)
})

这篇关于CouchDB 视图 - 在键数组上过滤和分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆