0

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?

1 Answer 1

1

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)});
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your response. These both seem to work and give me VERY close to what I am looking for. However, I do not fully understand the code; can you explain how these work?
forEach just iterate over recipients array and apply a function for extracting sentTS attribute of each array element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.