couchdb 文档更新处理程序单个字段

couchdb document update handler single field

我想创建一个设计文档,它将更新(文档的)单个字段,同时返回整个文档。在我的数据库中,文档示例如下:

{
    "domain": "google.it",
    "controlled":"0"
}

我想进行如下查询:

function(doc) {
    if(doc.controlled=="0") {
       doc['controlled']='1';
       return[doc];
    }
}

我读过 CouchDB 支持文档更新处理程序,但我无法创建一个。有人有初学者指南吗?

下面的代码暗示文档已经存在。

function(doc,req) {
    if (!doc) return [null,"Doc does not exists! Create it first."];
    doc.controlled = doc.controlled=="0"?"1":"0";
    return [doc,"Ok, Updated"]
}

您可以使用req.query.yourquerystringparameter根据请求查询字符串参数更新您的文档

阅读official documentation on update functions了解详情

你找到一个 easy to start guide here 或更高级的 couchdb tutorial here