CarCrazyBen
CarCrazyBen

Reputation: 1136

how to query CouchDB array

Our CouchDB contains many JSON documents with a nested Array, like this:

{ "_id": "3147cb0e74449e1c28c6ded2b4a3fa45e0d65481bd_RXMARTINEZ@miscemail.com_2017-11-30T13:38:33.955Z",
 "_rev": "3-99aef1458fe1a8f310c83156b9d06a69",
"delivery": {
 "application": "EnvSystem",
 "sender": {
  "id": "[email protected]",
  "type": "user"
 },
 "recipients": [
  {"type": "email",
   "recipient": "\"Artzer, Daniel J\" <[email protected]>",
   "sentTS": "2018-01-30T19:46:31.515Z",
   "id": "45281ab0-05f6-11e8-a86a-61a54dcb42aa"},
  {"type": "email",
   "recipient": "\"Hill, Robert V\" <[email protected]>",
   "sentTS": "2018-01-30T19:46:31.516Z",
   "id": "452841c0-05f6-11e8-a86a-61a54dcb42aa"},
  {"type": "email",
   "recipient": "\"Ledesma, Oscar\" <[email protected]>",
   "sentTS": "2018-01-30T19:46:31.516Z",
   "id": "452841c1-05f6-11e8-a86a-61a54dcb42aa"}

]

I have written a view which returns the entire array: emit(doc.delivery.recipients,1)

what I want is to only return the "sentTS" element within the array. How do I write my View do accomplish this?

Upvotes: 0

Views: 473

Answers (1)

Juanjo Rodriguez
Juanjo Rodriguez

Reputation: 2121

You can use this map function if you want to emit the startTS as the key array

function (doc) {
  ts = [];
  doc.delivery.recipients.forEach(function(e){ts.push(e.sentTS)});
  emit(ts,1);
}

or this if you want to emit a key for each sentTS

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

Upvotes: 1

Related Questions