Jamesjin
Jamesjin

Reputation: 371

is there way add new field/value in couchdb existed document

is there way add new field and its value into existed all couchdb documents, like following I want to add "emailType": "true" into the existed document for emails docType, not sure how can I use updatehandler to achieve this, thank

 {
       "_id": "37fbfee963e9a989becbe9fe746891eb",
       "_rev": "5-28bb00c72c0807772051d8d71b67eda0",
       "docType":"emails",
       "emails": [
           {
               "emailAddress": "aaaa",
               "emailType": "true"
           }
     }

here is what I get the existed email document

function(doc) {
    if (doc.emails && doc.docType == "emails") {
      var emailsLength = doc.emails.length;
      for (var i = 0; i < emailsLength ; i++) {
        emit(doc.emails[i].emailAddress, doc);
    }
  }
}

Upvotes: 0

Views: 932

Answers (1)

aleknaui
aleknaui

Reputation: 439

Unfortunately, you can't modify multiple documents at once with an update function. For that, you will have to write your own script that iterates on all the docs emited by the view you described.

Here's how to create update functions and how to call them in the docs. However, they are not the only way to modify documents. Maybe the bulk_docs operation is useful to you.

Upvotes: 1

Related Questions