Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 console the ID of each document that was removed.