0

I've been working a lot with Cradle, the couchDB client. However, I have a document filled with key-value pairs, and I'm trying to delete a specific row in there.

In the documentation, I cannot find a way to do deletion that doesn't include deleting the entire document or updating it with a null value. Can anyone point me in the right direction? I feel this is most likely a very simple issue that lots of people run into.

streetlight
  • 5,968
  • 13
  • 62
  • 101

2 Answers2

2

In CouchDB, there is no support for partial document updates (support for this is discussed every once in a while, but since there is no accepted way to patch JSON, it never gets very far), so you have to update the entire document with a copy where the unwanted key/value pair is removed.

djc
  • 11,603
  • 5
  • 41
  • 54
  • Ah, that's terrible. However, thank you for the clear and concise answer! Now off to writing all that code... lol – streetlight Mar 13 '13 at 21:43
0

Okay all, I found a solution that's a work around to this.

Instead of making a document that has a list of key-value pairs, instead just make a document with one field and for it's value insert the JSON of the key value pairs. That way, you can pull the value, delete a field and save it back to the original value, without needing to recreate the entire document over again.

Here's an example using Cradle:

db.get('document', function (err, doc) {
  var inside_key_values = doc.key_value_pair;

  delete inside_key_values[key_to_delete];

  db.merge("document", {
        key_value_pair: inside_key_values
      }, function (err, res) {
        console.log('New key value pairs saved')
    });

});

This is all based @djc 's great response on the lack of support on the couchDB end. Hopefully someone else out there finds this workaround valuable.

streetlight
  • 5,968
  • 13
  • 62
  • 101
  • Hmm, I don't think you understood my answer as I meant it, or maybe I misunderstood your question. Perhaps the Cradle API confuses the actual operations a little bit. – djc Mar 14 '13 at 14:21