Pouchdb 简明教程

PouchDB - Create Batch

您可以使用 db.bulkDocs() 方法在 PouchDB 中创建一个文档数组(批处理)。当使用该方法创建文档时,如果我们没有提供 db.bulkDocs() 值,PouchDB 会代表我们为批量中的全部文档生成唯一的 ID。

You can create an array (batch) of documents in PouchDB using the db.bulkDocs() method. While creating documents, using this method if we do not provide _id values, on our behalf PouchDB generates unique ids for all the documents in the bulk.

Syntax

下面是 PouchDB 的 db.bulkDocs() 方法使用的语法。您可以将所有要创建的 PouchDB 文档存储在一个数组中,并作为一个参数将它传递给该方法。此外,该方法还接受一个回调(可选)函数作为参数。

Following is the syntax of using the db.bulkDocs() method of PouchDB. You can store all the documents that are to be created in PouchDB in an array and pass it to this method as a parameter. In addition to it, this method also accepts a callback (optional) function as a parameter.

db.bulkDocs(docs, [options], [callback])

Example

下面是使用 db.bulkDocs () 方法在 PouchDB 中创建多个文档的示例。我们创建的文档应为 JSON 格式,一组用逗号( , )分隔的键值对,并用花括号( {} )括起来。

Following is an example of creating multiple documents in PouchDB using the db.bulkDocs () method. The documents 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 documents array
doc1 = {_id: '001', name: 'Ram', age: 23, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Robert', age: 24, Designation: 'Programmer'}
doc3 = {_id: '003', name: 'Rahim', age: 25, Designation: 'Programmer'}
docs = [doc1, doc2, doc3]

//Inserting Documents
db.bulkDocs(docs, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Documents created Successfully");
   }
});

将上述代码另存为名称为 Create_Batch.js 的文件中。打开命令提示符,并使用 node 执行该 JavaScript 文件,如下所示。

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

C:\PouchDB_Examples >node Create_Batch.js

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

This creates the given document in PouchDB database named my_database which is stored locally. The following message gets displayed.

Documents created Successfully

Inserting a Batch in a Remote Database

您可以在以远程方式存储在服务器(CouchDB)中的数据库中插入一个文档数组。

You can insert an array of documents in the database that is stored remotely on the server (CouchDB).

要执行此操作,您需要传递我们希望在 CouchDB 中创建文档的数据库的路径,而不是数据库名称。

To do so, instead of a database name you need to pass the path to the database where we 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 batch in remote database

下面是将一个文档数组插入保存到 CouchDB 服务器中的名为 my_database 的数据库中的示例。

Following is an example of inserting an array of documents in the database named my_database which 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 documents array

doc1 = {_id: '001', name: 'Ram', age: 23, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Robert', age: 24, Designation: 'Programmer'}
doc3 = {_id: '003', name: 'Rahim', age: 25, Designation: 'Programmer'}

docs = [doc1, doc2, doc3]

//Inserting Documents
db.bulkDocs(docs, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Documents created Successfully");
   }
});

将上述代码另存为名称为 Remote_Create_Batch.js 的文件中。打开命令提示符,并使用 node 执行该 JavaScript 文件,如下所示。

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

C:\PouchDB_Examples >node Remote_Create_Batch.js

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

This creates the given documents in PouchDB database named my_database which is stored in CouchDB. The following message is displayed.

Document created Successfully

Verification

在执行上述程序后,如果你再次访问 my_database ,可以看到如下所示创建的文档。

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

inserting batch verification