Ernesto Schiavo
Ernesto Schiavo

Reputation: 1060

couchdb document update handler single field

I want to create a design document which will make the update of a single field (of a document) while it gives back the entire document. In my database, an example of document is the following:

{
    "domain": "google.it",
    "controlled":"0"
}

I’d like to do a query such as:

function(doc) {
    if(doc.controlled=="0") {
       doc['controlled']='1';
       return[doc];
    }
}

I have read CouchDB supports the Document Update Handler but I can’t create one. Has someone any guide for beginners?

Upvotes: 0

Views: 1051

Answers (1)

giowild
giowild

Reputation: 479

Code below implies doc already exists.

function(doc,req) {
    if (!doc) return [null,"Doc does not exists! Create it first."];
    doc.controlled = doc.controlled=="0"?"1":"0";
    return [doc,"Ok, Updated"]
}

You can use req.query.yourquerystringparameter to update your doc based on request query string parameters

Read official documentation on update functions for details

You find an easy to start guide here or a more advanced couchdb tutorial here

Upvotes: 1

Related Questions