Pouchdb 简明教程

PouchDB - Create Document

您可以使用 db.put() 方法在 PouchDB 中创建文档。

You can create a document in PouchDB using the db.put() method.

Syntax

以下是 PouchDB 的 db.put() 方法的使用语法。您可以将要创建的文档存储在 PouchDB 中,将其存储在变量中,然后作为参数传递给此方法。此外,此方法还将回调(可选)函数作为参数进行接收。

Following is the syntax of using the db.put() method of PouchDB. You can store the document that is to be created in PouchDB, in a variable and pass as a parameter to this method. In addition, this method also accepts a callback (optional) function as a parameter.

db.put(document, callback)

Example

以下是如何使用 put() 方法在 PouchDB 中创建文档的示例。我们创建的文档应该是 JSON 格式,用逗号 ( , ) 分隔的键值对的集合,并用大括号 ( {} ) 括起来。

Following is an example of creating a document in PouchDB using the put() method. The document we create should be of JSON format, a set of key-value pairs separated by comma (,) and enclosed within curly braces ({}).

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = new PouchDB('my_database');

//Preparing the document
doc = {
   _id : '001',
   name: 'Raju',
   age : 23,
   designation : 'Designer'
   }
//Inserting Document
db.put(doc, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Document created Successfully");
   }
});

将以上代码保存在名为 Create_Document.js 的文件中。打开命令提示符,使用 node 执行 JavaScript 文件,如下所示。

Save the above code in a file with name Create_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

C:\PouchDB_Examples >node Create_Document.js

这会在名为 my_database 的 PouchDB 数据库中创建给定的文档,该数据库存储在本地,显示以下消息。

This creates the given document in PouchDB database named my_database, which is stored locally, displaying the following message.

Document created Successfully

Inserting a Document in a Remote Database

您还可以在服务器(CouchDB)上远程存储的数据库中插入文档。

You can also insert a document in the database that is stored remotely on the server (CouchDB).

为此,您需要传递文档所在数据库的路径,而不是数据库名称,以在 CouchDB 中创建文档。

To do so, instead of database name you need to pass the path to the database where you want to create documents in CouchDB.

Example

假设 CouchDB 服务器中有一个名为 my_database 的数据库。然后,如果您使用 URL http://127.0.0.1:5984/_utils/index.html 验证 CouchDB 中的数据库列表,您将获得以下屏幕截图。

Suppose there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB using the URL http://127.0.0.1:5984/_utils/index.html you will get the following screenshot.

inserting document in remote database

现在,如果您单击名为 my_database 的数据库,您会发现一个空数据库,如以下屏幕截图所示。

Now, if you click on the database named my_database, you will find an empty database as shown in the following screenshot.

empty database

以下是将在保存在 CouchDB 服务器中的名为 my_database 的数据库中插入文档的示例。

Following is an example of inserting a document in a database named my_database that is saved in the CouchDB server.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = new PouchDB('http://localhost:5984/my_database');

//Preparing the document
doc = {
   _id : '001',
   name: 'Raju',
   age : 23,
   designation : 'Designer'
   }
//Inserting Document
db.put(doc, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Document created Successfully");
   }
});

将以上代码保存在名为 Remote_Create_Document.js 的文件中。打开命令提示符,使用 node 执行 JavaScript 文件,如下所示。

Save the above code in a file with the name Remote_Create_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

C:\PouchDB_Examples >node Remote_Create_Document.js

这会在名为 my_database 的 PouchDB 数据库中创建给定的文档,该数据库存储在 CouchDB 中,显示以下消息。

This creates the given document in PouchDB database named my_database which is stored in CouchDB, displaying the following message.

Document created Successfully

Verification

执行上述程序后,如果您再次访问 my_database ,您会看到创建的文档,如以下屏幕截图所示。

After executing the above program, if you visit the my_database again, you can observe the document created as shown in the following screenshot.

remote database verification