如何查询 CouchDB 数组
how to query CouchDB array
我们的 CouchDB 包含许多 JSON 个带有嵌套数组的文档,如下所示:
{ "_id": "3147cb0e74449e1c28c6ded2b4a3fa45e0d65481bd_RXMARTINEZ@miscemail.com_2017-11-30T13:38:33.955Z",
"_rev": "3-99aef1458fe1a8f310c83156b9d06a69",
"delivery": {
"application": "EnvSystem",
"sender": {
"id": "RXMARTINEZ@miscemail.com",
"type": "user"
},
"recipients": [
{"type": "email",
"recipient": "\"Artzer, Daniel J\" <DJArtzer@miscemail.com>",
"sentTS": "2018-01-30T19:46:31.515Z",
"id": "45281ab0-05f6-11e8-a86a-61a54dcb42aa"},
{"type": "email",
"recipient": "\"Hill, Robert V\" <RVHill@miscemail.com>",
"sentTS": "2018-01-30T19:46:31.516Z",
"id": "452841c0-05f6-11e8-a86a-61a54dcb42aa"},
{"type": "email",
"recipient": "\"Ledesma, Oscar\" <OLedesma@miscemail.com>",
"sentTS": "2018-01-30T19:46:31.516Z",
"id": "452841c1-05f6-11e8-a86a-61a54dcb42aa"}
]
我写了一个 return 整个数组的视图:
emit(doc.delivery.recipients,1)
我想要的只是 return 数组中的 "sentTS" 元素。我如何编写我的视图来完成此操作?
如果你想发出 startTS 作为键数组,你可以使用这个映射函数
function (doc) {
ts = [];
doc.delivery.recipients.forEach(function(e){ts.push(e.sentTS)});
emit(ts,1);
}
或者如果你想为每个发送的 TS 发出一个密钥
function (doc) {
doc.delivery.recipients.forEach(function(e){emit(e.sentTS)});
}
我们的 CouchDB 包含许多 JSON 个带有嵌套数组的文档,如下所示:
{ "_id": "3147cb0e74449e1c28c6ded2b4a3fa45e0d65481bd_RXMARTINEZ@miscemail.com_2017-11-30T13:38:33.955Z",
"_rev": "3-99aef1458fe1a8f310c83156b9d06a69",
"delivery": {
"application": "EnvSystem",
"sender": {
"id": "RXMARTINEZ@miscemail.com",
"type": "user"
},
"recipients": [
{"type": "email",
"recipient": "\"Artzer, Daniel J\" <DJArtzer@miscemail.com>",
"sentTS": "2018-01-30T19:46:31.515Z",
"id": "45281ab0-05f6-11e8-a86a-61a54dcb42aa"},
{"type": "email",
"recipient": "\"Hill, Robert V\" <RVHill@miscemail.com>",
"sentTS": "2018-01-30T19:46:31.516Z",
"id": "452841c0-05f6-11e8-a86a-61a54dcb42aa"},
{"type": "email",
"recipient": "\"Ledesma, Oscar\" <OLedesma@miscemail.com>",
"sentTS": "2018-01-30T19:46:31.516Z",
"id": "452841c1-05f6-11e8-a86a-61a54dcb42aa"}
]
我写了一个 return 整个数组的视图:
emit(doc.delivery.recipients,1)
我想要的只是 return 数组中的 "sentTS" 元素。我如何编写我的视图来完成此操作?
如果你想发出 startTS 作为键数组,你可以使用这个映射函数
function (doc) {
ts = [];
doc.delivery.recipients.forEach(function(e){ts.push(e.sentTS)});
emit(ts,1);
}
或者如果你想为每个发送的 TS 发出一个密钥
function (doc) {
doc.delivery.recipients.forEach(function(e){emit(e.sentTS)});
}