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 ...

Get JavaScript JSON Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.