Deleting a document in CouchDB using Node.js and Cradle

To remove a record, you use the Cradle module's remove method and pass the ID of the document you want to remove.

How to do it...

Here's an example of remove:

db.remove(id);

Passing an ID removes the document with the given ID.

There's more…

If you have more than one document to remove, you could iterate across all documents, the way the following code does, removing each document in turn:

db.all(function(err, doc) {
  for(var i = 0; i < doc.length; i++) {
    db.remove(doc[i].id, doc[i].value.rev, function(err, doc) {
      console.log('Removing ' + doc._id);
    });
  }
});

This is a more complex use of remove; it takes the document's ID, the revision of the document, and a callback function, which logs to the

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.