Updating a document in CouchDB with Node.js and Cradle
The Cradle module defines the merge
method to let you update an existing document.
How to do it...
Here's an example where we change the call of a record from kf6gpe-7
to kf6gpe-9
by specifying its ID, and then performing a merge with the new data:
var call = "kf6gpe-7"; db.merge(id, {call: 'kf6gpe-9'}, function(error, doc) { db.get(id, function(error, doc) { console.log(doc); }); });
As you can see from the function, merge
takes the ID of the record to merge, and a JavaScript object with the fields to replace or add to the existing object. You can also pass a callback, which is invoked by merge when the operation completes. The error value will be non-zero in the event of an error, and the document is returned as the second argument. Here, we just log the contents of the revised document to the console.