1

我需要从 CouchDB 中删除一个文档,但首先我需要检查是否满足条件。首先想到的是使用更新处理程序,手动迭代更新处理程序中的所有键并删除没有_前缀的键并将_delete字段设置为true.

我想这应该产生与 normal 相同的结果DELETE,对吗?

更新:

使用@Kxepal 的建议,更新处理程序如下所示:

function (doc, req) {
  if (req.query.account_id != doc.account_id) {
    return [doc, '{"error": "invalid account id"}'];
  }
  return [{
    _id: doc._id,
    _rev: doc._rev,
    _deleted: true
  }, 'ok'];
}
4

1 回答 1

0

并不真地。

当您DELETE记录时,CouchDB 会从中删除除_idand之外的所有字段_rev(当然,_deleted也存在)。此信息也称为tombstone.

当您只是添加_deleted: true到文档时,它会被标记为已删除,但所有其他字段仍然存在:

http --json http://localhost:5984/recipes/SpaghettiWithMeatballs

HTTP/1.1 404 Object Not Found
Cache-Control: must-revalidate
Content-Length: 41
Content-Type: application/json
Date: Sat, 17 Aug 2013 14:49:17 GMT
Server: CouchDB/1.4.0+build.c843cef (Erlang OTP/R16B)

{
    "error": "not_found", 
    "reason": "deleted"
}

http --json http://localhost:5984/recipes/SpaghettiWithMeatballs\?rev\=9-ac84157c2206793b7d142f5d8e2c97d0

HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 425
Content-Type: application/json
Date: Sat, 17 Aug 2013 14:48:55 GMT
ETag: "9-ac84157c2206793b7d142f5d8e2c97d0"
Server: CouchDB/1.4.0+build.c843cef (Erlang OTP/R16B)

{
    "_attachments": {
        "my_recipe.txt": {
            "content_type": "text/plain", 
            "digest": "md5-198BPPNiT5fqlLxoYYbjBA==", 
            "length": 85, 
            "revpos": 2, 
            "stub": true
        }
    }, 
    "_deleted": true, 
    "_id": "SpaghettiWithMeatballs", 
    "_rev": "9-ac84157c2206793b7d142f5d8e2c97d0", 
    "description": "An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.", 
    "ingredients": [
        "spaghetti", 
        "tomato sauce", 
        "meatballs"
    ], 
    "name": "Spaghetti with meatballs"
}

但是,在压缩过程中,该文档将被转换为tombstone只有_id,_rev_deleted字段。在此之前,这是将数据保留在文档中以轻松撤消删除操作而无需请求文档的先前修订版的好技巧,因为所有内容都可用于最新版本。

于 2013-08-17T14:52:16.263 回答