Couchdb 简明教程

CouchDB - Deleting a Document

Deleting a Document using cURL Utility

你可以通过使用 cURL 实用工具通过 DELETE 方法向服务器发送 HTTP 请求,在 CouchDB 中删除文档。以下是删除文档的语法。

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

curl -X DELETE http : // 127.0.0.1:5984 / database name/database id?_rev id

使用 −X, ,我们可以指定与 HTTP 服务器通信时正在使用的自定义 HTTP 请求方法。在本例中,我们正在使用 Delete 方法。删除数据库 /database_name/database_id/ 不够。你必须通过 URL 传递最近的版本 ID。若要提及任何数据结构的属性,请使用 "?"

Using −X, we can specify a custom request method of HTTP we are using, while communicating with the HTTP server. In this case, we are using Delete method. To delete a database /database_name/database_id/ is not enough. You have to pass the recent revision id through the url. To mention attributes of any data structure "?" is used.

Example

假设名为 my_database 的数据库中有一个文档,其文档 ID 为 001。要删除此文档,你必须获取该文档的修订版 ID。按照如下所示获取文档数据。

Suppose there is a document in database named my_database with document id 001. To delete this document, you have to get the rev id of the document. Get the document data as shown below.

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

现在指定待删除的文档的版本 ID、文档 ID 以及文档所属的数据库名称,如下所示:

Now specify the revision id of the document to be deleted, id of the document, and database name the document belongs to, as shown below −

$ curl -X DELETE http://127.0.0.1:5984/my_database/001?rev=1-
3fcc78daac7a90803f0a5e383f4f1e1e

{"ok":true,"id":"001","rev":"2-3a561d56de1ce3305d693bd15630bf96"}

Verification

若要验证文档是否被删除,请尝试使用 GET 方法获取文档。由于你要获取已删除的文档,这会给你一个错误消息,如下所示:

To verify whether the document is deleted, try to fetch the document by using the GET method. Since you are fetching a deleted document, this will give you an error message as shown below −

$ curl -X GET http://127.0.0.1:5984/my_database/001
{"error":"not_found","reason":"deleted"}

Deleting a Document using Futon

首先,验证数据库中的文档。以下是名为 tutorials_point 的数据库的快照。

First of all, verify the documents in the database. Following is the snapshot of the database named tutorials_point.

deleting document

你可以在此观察到,数据库包含三个文档。若要删除其中任何文档,如 003, ,请执行以下操作:

Here you can observe, the database consists of three documents. To delete any of the documents say 003, do the following −

  1. Click on the document, you will get a page showing the contents of selected document in the form of field-value pairs.

  2. This page also contains four options namely Save Document, Add Field, Upload Attachment, Delete Document.

  3. Click on Delete Document option.

  4. You will get a dialog box saying "Are you sure you want to delete this document?" Click on delete, to delete the document.

delete document2