O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required