Pouchdb 简明教程

PouchDB - Delete Batch

您可以使用 bulkDocs() 方法一次性删除 PouchDB 中的文档数组。这样做,您需要创建一个要删除的文档数组,其中每个文档应该包含 _id_rev 。除了这些外,您还得添加另外一个键值对 _deleted: true

You can delete an array of documents in PouchDB at once using the bulkDocs() method. To do so you need to create an array of documents that are to be deleted where, each document should contain _id and _rev. In addition to these you have to add another key-value pair _deleted: true.

假设存储在 PouchDB 本地数据库中的名为 my_database 的数据库包含 3 个文档,即 doc1、doc2、doc3,内容如下。

Suppose the database named my_database that is stored locally in PouchDB contains 3 documents namely doc1, doc2, doc3 with the following contents.

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'}

并且,假设我们需要删除所有三个文档。那么,您首先需要获取它们的 _rev 值。因此,使用以下代码获取这些文档的内容。

And say, we have to delete all the three documents. Then, first of all you need to get their _rev values. Therefore, fetch the contents of these documents using the following code.

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

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

//Retrieving all the documents in PouchDB
db.allDocs({include_docs: true},function(err, docs) {
   if (err) {
      return console.log(err);
   } else {
      console.log(docs.rows);
   }
});

将上述代码另存为 bulk_fetch.js 。执行上述程序后,将获得数据库中文档的 _id_rev 值,如下所示。

Save the above code as bulk_fetch.js. Executing the above program gives you the _id and _rev values of the documents in the database as shown below.

[
   {
      id: '001',
      key: '001',
      value: { rev: '1-1604b0c3ff69dc1e261265fd60808404' }
   },
   {
      id: '002',
      key: '002',
      value: { rev: '1-b5e49db7e984841bf12a13e3ee548125' }
   },
   {
      id: '003',
      key: '003',
      value: { rev: '1-a7b342786ecc707aa91f3b321a177b51' }
   }
]

现在,您可以使用各自的 _id_rev 值删除文档,如下所示。

Now, you can delete the documents using their respective _id and _rev values as shown below.

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

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

//Preparing the document
docs = [{_id : '001', _rev: '2-77f3a9974dd578d12f3f2a33aae64c8d', _deleted : true },
      {_id : '002', _rev: '2-43966007568ce9567c96422195fcfa0d', _deleted : true },
      {_id : '003', _rev: '2-6c5349652527f4f39583ff14f23cd677',_deleted : true }]

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

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

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

C:\PouchDB_Examples >node Delete_All_Document.js

这将删除名为 my_database 的数据库中存在的全部文档,该数据库以本地存储,并显示以下消息。

This deletes all the documents that exists in the database named my_database which is stored locally, displaying the following message.

Documents Deleted Successfully

现在,如果您执行 bulk_fetch.js 程序,则您会在控制台上看到一个空的大括号,表示该数据库为空,如下所示。

Now, if you execute the bulk_fetch.js program, you can observe an empty brace on the console indicating that the database is empty, as shown below.

[]

Deleting Batch from a Remote Database

您可以更新以远程方式存储在服务器(CouchDB)中的数据库中的全部文档。

You can update all the documents from 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 in CouchDB, which contains the document that is to be read.

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.

deleting batch from remote database

如果我们选择名为 my_database 的数据库,则可以观察到它包含 3 个文档,如以下屏幕截图所示。

If we select the database named my_database, you can observe that it contains 3 documents as shown in the following screenshot.

deleting batch

下面是删除存储在 CouchDB 服务器中的名为 my_database 的数据库中存在的所有文档的示例。

Following is an example of deleting all the documents that exist in a database named my_database which is stored 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
docs = [{_id : '001', _rev: '4-6bc8d9c7a60fed2ed1667ec0740c1f39', _deleted : true },
      {_id : '002', _rev: '2-1aa24ce77d96bb9d2a0675cdf1e113e0', _deleted : true },
      {_id : '003', _rev: '2-fa113149ba618eda77f73072974a2bc1',_deleted : true }]

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

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

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

C:\PouchDB_Examples >node Remote_Delete_AllDocuments.js

这将删除存储在 CouchDB 中的名为 my_database 的数据库中存在的全部给定文档的内容,并显示以下消息。

This deletes the contents of all given document that exists in the database named my_database which is stored in CouchDB, and displays the following message.

Documents Deleted Successfully