Couchdb 简明教程

CouchDB - Creating a Document

文档是 CouchDB 的中心数据结构。数据库的内容将以文档形式而不是表的形式存储。您可以使用 CouchDB 和 Futon 提供的 cURL 实用程序来创建这些文档。本章涵盖在数据库中创建文档的方法。

Documents are CouchDB’s central data structure. Contents of the database will be stored in the form of Documents instead of tables. You can create these documents using cURL utility provided by CouchDB, as well as Futon. This chapter covers the ways to create a document in a database.

CouchDB 中的每个文档都有一个唯一的 ID。您可以选择您自己的 ID,它应该是字符串形式。通常使用 UUID(通用唯一标识符),它们是随机数,产生重复的可能性最小。首选这些以避免冲突。

Each document in CouchDB has a unique ID. You can choose your own ID that should be in the form of a string. Generally, UUID (Universally Unique IDentifier) is used, which are random numbers that have least chance of creating a duplicate. These are preferred to avoid collisions.

Creating a Document using cURL Utility

您可以使用 PUT 方法向服务器发送 HTTP 请求,通过 cURL 实用程序在 CouchDB 中创建一个文档。以下是在 CouchDB 中创建文档的语法:

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

$ curl -X PUT http://127.0.0.1:5984/database name/"id" -d ' { document} '

使用 −X ,我们可以指定在与 HTTP 服务器通信时使用的 HTTP 自定义请求方法。在此情况下,我们使用 PUT 方法。当我们使用 PUT 方法时,url 的内容指定我们使用 HTTP 请求创建的对象名称。此处我们必须发送以下内容:

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 PUT method. When we use the PUT method, the content of the url specifies the object name we are creating using the HTTP request. Here we have to send the following −

  1. The name of the database name in which we are creating the document.

  2. The document id.

  3. The data of the document. −d option is used to send the data/document through HTTP request. While writing a document simply enter your Field-Value pairs separated by colon, within flower brackets as shown below −

{
   Name : Raju
   age : 23
   Designation : Designer
}

Example

使用上述给出的语法,如果你想在名称为 my_database 的数据库中使用 id 001 创建文档,那么你可以按照如下所示进行创建。

Using the above given syntax if you want to create a document with id 001 in a database with name my_database, you can create it as shown below.

$ curl -X PUT http://127.0.0.1:5984/my_database/"001" -d
'{ " Name " : " Raju " , " age " :" 23 " , " Designation " : " Designer " }'

{"ok":true,"id":"001","rev":"1-1c2fae390fa5475d9b809301bbf3f25e"}

CouchDB 对此请求的响应包含三个字段 −

The response of CouchDB to this request contains three fields −

  1. "ok", indicating the operation was successful.

  2. "id", which stores the id of the document and

  3. "rev", this indicates the revision id. Every time you revise (update or modify) a document a _rev value will be generated by CouchDB. If you want to update or delete a document, CouchDB expects you to include the _rev field of the revision you wish to change. When CouchDB accepts the change, it will generate a new revision number. This mechanism ensures concurrency control.

Verification

如果你想查看创建的文档,可以使用文档按如下所示获取。

If you want to view the created document you can get it using the document as shown below.

$ curl -X GET http://127.0.0.1:5984/my_database/001
{
   "_id": "001",
   "_rev": "1-3fcc78daac7a90803f0a5e383f4f1e1e",
   "Name": "Raju",
   "age": 23,
   "Designation": "Designer"
}

Creating a Document using Futon

要创建文档,请打开 http://127.0.0.1:5984/_utils/ url 以获取 CouchDB 的概述/索引页面,如下所示。

To Create 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

选择你要在其中创建文档的数据库。打开数据库的概述页面,然后选择 New Document 选项,如下所示。

Select the database in which you want to create the document. Open the Overview page of the database and select New Document option as shown below.

new document

当你选择 New Document 选项时,CouchDB 创建一个新的数据库文档,分配给它一个新的 id。你可以编辑 id 的值,并且可以以字符串的形式分配你自己的值。在下面的插图中,我们使用 id 001 创建了一个新文档。

When you select the New Document option, CouchDB creates a new database document, assigning it a new id. You can edit the value of the id and can assign your own value in the form of a string. In the following illustration, we have created a new document with an id 001.

new document id

在此页面,你可以观察到三个选项 − 保存文档、添加字段和上传附件。

In this page, you can observe three options − save Document, Add Field and Upload Attachment.

Add Field to the Document

要向文档中添加字段,请单击 Add Field 选项。在创建数据库之后,你可以使用此选项向其中添加字段。单击它将获得一对文本框,即 Field, value. 你可以单击它们来编辑这些值。编辑这些值并输入你想要的字段-值对。单击绿色按钮保存这些值。

To add field to the document click on Add Field option. After creating a database, you can add a field to it using this option. Clicking on it will get you a pair of text boxes, namely, Field, value. You can edit these values by clicking on them. Edit those values and type your desired Field-Value pair. Click on the green button to save these values.

在下面的插图中,我们创建了员工的三个字段姓名、年龄和职务。

In the following illustration, we have created three fields Name, age and, Designation of the employee.

create field

Save Document

你可以单击此选项保存对文档所做的更改。保存后,将生成一个新的 id _rev ,如下所示。

You can save the changes made to the document by clicking on this option. After saving, a new id _rev will be generated as shown below.

save document