如何查询CouchDB数组

时间:2018-01-31 18:36:04

标签: arrays couchdb

我们的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"}

我写了一个返回整个数组的视图: emit(doc.delivery.recipients,1)

我想要的只是返回数组中的“sentTS”元素。如何编写我的View才能做到这一点?

1 个答案:

答案 0 :(得分:1)

如果要将startTS作为键阵列

发出,可以使用此映射功能
function (doc) {
  ts = [];
  doc.delivery.recipients.forEach(function(e){ts.push(e.sentTS)});
  emit(ts,1);
}

或者如果你想为每个sentTS发出一个密钥

function (doc) {
 doc.delivery.recipients.forEach(function(e){emit(e.sentTS)});
}