简体   繁体   English

如何使用CouchDB / Node.js中的Cradle删除键值对?

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

I've been working a lot with Cradle, the couchDB client. 我一直在使用couchDB客户端Cradle工作。 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. 我觉得这很可能是很多人遇到的一个非常简单的问题。

在CouchDB中,不支持部分文档更新(偶尔会讨论对此的支持,但由于没有可接受的方法来修补JSON,因此它永远不会很远),因此您必须更新整个文档删除不需要的键/值对的副本。

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. 而不是创建具有键值对列表的文档,而只是创建一个包含一个字段的文档,并为其值插入键值对的JSON。 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: 以下是使用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. 这完全基于@djc对couchDB端缺乏支持的响应。 Hopefully someone else out there finds this workaround valuable. 希望那里的其他人发现这种解决方法很有价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM