Couchdb 简明教程

CouchDB - Updating a Document

Updating Documents using cURL

你可以使用 cURL 实用程序通过使用 PUT 方法向服务器发送 HTTP 请求来更新 CouchDB 中的文档。以下是对文档进行更新的语法。

You can update a document in CouchDB by sending an HTTP request to the server using PUT method through cURL utility. Following is the syntax to update a document.

curl -X PUT http://127.0.0.1:5984/database_name/document_id/ -d '{ "field" : "value", "_rev" : "revision id" }'

Example

假设在名为 my_database 的数据库中有一个 id 为 001 的文档。你可以按照如下所示删除此内容。

Suppose there is a document with id 001 in the database named my_database. You can delete this as shown below.

首先,获取要更新的文档的修订 id。你可以在文档本身中找到文档的 _rev ,因此按如下所示获取文档。

First of all, get the revision id of the document that is to be updated. You can find the _rev of the document in the document itself, therefore get the document as shown below.

$ curl -X GET http://127.0.0.1:5984/my_database/001
{
   "_id" : "001",
   "_rev" : "2-04d8eac1680d237ca25b68b36b8899d3 " ,
   "age" : "23"
}

从文档中使用修订 id _rev 更新文档。在这里,我们将年龄从 23 更新为 24。

Use revision id _rev from the document to update the document. Here we are updating the age from 23 to 24.

$ curl -X PUT http://127.0.0.1:5984/my_database/001/ -d
' { " age " : " 24 " , " _rev " : " 1-1c2fae390fa5475d9b809301bbf3f25e " } '

{ " ok " : true , " id " : " 001 " , " rev " : " 2-04d8eac1680d237ca25b68b36b8899d3 " }

Verification

若要验证文档,请按如下所示使用 GET 请求再次获取文档。

To verify the document, get the document again using GET request as shown below.

$ curl -X GET http://127.0.0.1:5984/my_database/001
{
   " _id " : " 001 ",
   " _rev " : " 2-04d8eac1680d237ca25b68b36b8899d3 " ,
   " age " : " 23 "
 }

以下是更新文档时需要指出的几个要点。

Following are some important points to be noted while updating a document.

  1. The URL we send in the request containing the database name and the document id.

  2. Updating an existing document is same as updating the entire document. You cannot add a field to an existing document. You can only write an entirely new version of the document into the database with the same document ID.

  3. We have to supply the revision number as a part of the JSON request.

  4. In return JSON contains the success message, the ID of the document being updated, and the new revision information. If you want to update the new version of the document, you have to quote this latest revision number.

Updating Documents using Futon

若要删除文档,请打开 http://127.0.0.1:5984/_utils/ URL 以获取如下图所示的 CouchDB 的概述/索引页面。

To delete a document open the http://127.0.0.1:5984/_utils/ url to get an Overview/index page of CouchDB as shown below.

create document

选择存在待更新文档的数据库并单击它。我们在此更新名为 tutorials_point 的数据库中的一个文档。你将获得数据库中的文档列表,如下图所示。

Select the database in which the document to be updated exists and click it. Here we are updating a document in the database named tutorials_point. You will get the list of documents in the database as shown below.

update document

选择要更新的文档并单击它。你将获得文档的内容,如下图所示。

Select a document that you want to update and click on it. You will get the contents of the documents as shown below.

document contents

在此,若要将位置从德里更新为海得拉巴,请单击文本框,编辑字段,然后单击绿色按钮以保存更改,如下图所示。

Here, to update the location from Delhi to Hyderabad, click on the text box, edit the field, and click the green button to save the changes as shown below.

save changes