CouchDB文档更新处理程序(就地更新) [英] CouchDB Document Update Handlers (in-place updates)
问题描述
http://wiki.apache.org/couchdb/Document_Update_Handlers
CouchDB(0.10及更高版本)现在支持就地更新。我无法理解其工作原理。我尝试使用提供的示例,但无法正常工作。
CouchDB ( 0.10 and above ) supports in-place updates now. I'm having trouble understanding how it works. I tried to use the example provided but I couldn't get it to work.
有人可以提供一些示例和uri用于访问就地更新。
Can someone provide some examples and uris used to access the in-place updates.
谢谢
推荐答案
示例函数 in- place
与其他
数据库中的就地更新不同。 CouchDB仍然使用仅追加的体系结构。文档更新
处理程序仍会创建新的文档修订版,等等。
The example function in-place
is not the same as "in-place" updates in other
databases. CouchDB still uses an append-only architecture; document update
handlers still create a new doc revision, etc.
不过,更新处理程序非常方便,值得学习。
Still, update handlers are quite convenient and worth learning.
假设您有一个带有累加器的文档。您
希望仅通过一个HTTP查询在文档中累积一个整数,并使用 amount
参数指定
增量。请考虑以下命令:
Suppose you have a document with an accumulator. You
want to accumulate an integer in a document with just one HTTP query, specifying
the increment amount using an amount
parameter. Consider the following commands:
curl -X PUT http://localhost:5984/db
# Returns {"ok":true}
curl -H "Content-Type:application/json" -X POST http://localhost:5984/db/_bulk_docs -d @-
{"docs":
[
{"_id": "my_doc", "number": 23},
{"_id": "_design/app",
"updates": {
"accumulate": "function (doc, req) {
var inc_amount = parseInt(req.query.amount);
doc.number = doc.number + inc_amount;
return [doc, \"I incremented \" +
doc._id + \" by \" +
inc_amount];
}"
}
}
]
}
# Returns [{"id":"my_doc","rev":"1-8c9c19a45a7e2dac735005bbe301eb15"},
# {"id":"_design/app","rev":"1-83ec85978d1ed32ee741ce767c83d06e"}]
(请记住t o在POST中的JSON对象后按文件结尾^ D。)
(Remember to press end-of-file, ^D, after the JSON object in the POST.)
下一步,确认要累积的文档( my_doc
)存在:
Next confirm the document for accumulation (my_doc
) exists:
curl http://localhost:5984/db/my_doc
# Returns {"_id":"my_doc","_rev":"1-8c9c19a45a7e2dac735005bbe301eb15",
# "number":23}
现在,您可以使用 amount
参数调用累积
更新处理程序b $ b更新该字段。
Now you can call the accumulate
update handler with an amount
parameter to
update the field.
curl -X PUT \
http://localhost:5984/db/_design/app/_update/accumulate/my_doc?amount=15
# Returns: I incremented my_doc by 15
curl http://localhost:5984/db/my_doc
# Returns {"_id":"my_doc","_rev":"2-<whatever>",
# "number":38}
请注意,新的数字
值为38,即23 + 15。
Notice that the new number
value is 38, the value of 23 + 15.
这篇关于CouchDB文档更新处理程序(就地更新)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!