2
votes

This may be a basic question but I've looked through the github Cloudant library and the Cloudant documentation and deleting a specific document from the database is mentioned but never thoroughly explained. It's very frustrating. The closest I've gotten to deleting a document is using an http request rather then the functions Cloudant library offers and I continuously get a "Document Update Conflict" even though I'm passing through the _rev of the document. Can anybody explain deleting a document from a Cloudant database using nodejs with an example to help sort this out. Thanks.

3

3 Answers

4
votes

You can use the destroy method of nano like @JakePeyser has said, instead of using http APIs since you are using nodejs. But since you are sending _rev and getting a "Document Update Conflict" error, it leads me to doubt if you have the latest _rev with you. "Document Update Conflict" happens mostly if the local _rev doesn't match the remote _rev. I would therefore suggest wrapping your destroy function in a get function. So an update to @JakePeyser's example would be:

var nano = require("nano")("cloudantURL"),
db = nano.db.use("yourDB");
db.get(docUniqueId, function(err, body) {
  if (!err) {
    var latestRev = body._rev;
    db.destroy(docUniqueId, latestRev, function(err, body, header) {
      if (!err) {
          console.log("Successfully deleted doc", docUniqueId);
      }
    });
  }
})
3
votes

It depends what node module you are using for communicating with Cloudant. With the nano driver, you can use the destroy method to delete a document. See the following code example:

var nano = require("nano")("cloudantURL"),
  db = nano.db.use("yourDB");
db.destroy(docUniqueId, docRevNum, function(err, body, header) {
  if (!err) {
    console.log("Successfully deleted doc", docUniqueId);
  }
});

Key

  • cloudantURL - URL of your Cloudant instance, with username and password embedded
  • yourDB - your database name
  • docUniqueId - Unique ID of the doc you want to delete
  • docRevNum - Revision number of the doc you want to delete
1
votes

Sample script to delete/destroy a doc from a collection "mytable" based on the value of the field "fkId".

var Cloudant = require('cloudant');
var Config = require('config-js');
var config = new Config('./settings.js');
var username = config.get('CLOUDANT_USER');
var password = config.get('CLOUDANT_PASWORD');
var cloudant = Cloudant({account:username, password:password});
var db = cloudant.db.use('mytable');
var key = 'fkId';
var value = '11234567890';
...
...
db.list({
  'include_docs': true
}, function (err, body) {
  /* istanbul ignore next */
  if (err)
      res.json({success: false, msg: 'Unable to fetch documents'});
  else {
      var rows = body.rows;
      var items = [];
      var rec_found = false;
      rows.forEach(function (row) {
        if (row.doc[key] === value) {
          rec_found = true;
          items.push(row.doc);
        }
      });
      if (items.length === 0) {
        console.log("No record found with fkId: "+ value);
        res.json({success: true, msg: 'No record found with fkId: '+ value});
      } else {
        var docId = items[0]._id;
        var docRev = items[0]._rev;
        db.destroy(docId, docRev,  function(err) {
          if (!err) {
            console.log("Successfully deleted doc with fkId: "+ value);
            res.json({success: true, msg: 'Successfully deleted the item from the database.'});
          } else {
            res.json({success: false, msg: 'Failed to delete with fkId from the database, please try again.'});
          }
        });
      }
  }
});