68733

How do I delete a key-value pair using Cradle in CouchDB/Node.js?

Question:

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 <a href="https://github.com/cloudhead/cradle" rel="nofollow">documentation,</a> 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.

Answer1:

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.

Answer2:

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.

Recommend

  • How to filter entities that are deleted using linq to entities
  • how to calculate distance between two points in google maps?
  • Change Divider Color Android DatePicker Dialog
  • Update SQL MS Access 2010
  • Put percent labels next to legend instead of in the slice
  • HABTM associations in Rails : collecting and counting the categories of a model's children
  • CSS - how to trim text output?
  • Google OAuth2 for an web application hosted behind NAT (intranet server without public IP)
  • Bootstrap collapse within a foreach loop
  • Group variable in cobol
  • phpmailer - How to verify a sent email arrived at its destination
  • R h2o.glm - issue with max_active_predictors
  • Is it safe to drop the -webkit vendor prefix from the css3 border-radius yet?
  • How do I translate LR(1) Parse into a Abstract syntax tree?
  • Separate ID and Class for JS and CSS
  • Guava how to copy all files from one directory to another
  • Are there any side effects from calling SQLAlchemy flush() within code?
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • Unable to install Git-core+svn by MacPorts
  • How can I speed up CURL tasks?
  • Moving Android View and preventing onDraw to be called over and over again
  • Django simple Captcha “No module named fields” error
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • Installing iPhone App to iPhone
  • Abort upload large uploads after reading headers
  • Could not find rake using whenever rails
  • Display issues when we change from one jquery mobile page to another in firefox
  • Android screen density dpi vs ppi
  • Different response to non-authenticated users and AJAX calls
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Data Validation Drop Down Box Arrow Disappearing
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • need help with bizarre java.net.HttpURLConnection behavior
  • LevelDB C iterator
  • Checking variable from a different class in C#
  • How can i traverse a binary tree from right to left in java?