Pouchdb 简明教程

PouchDB - Deleting Attachment

您可以使用 removeAttachment() 方法从 PouchDB 删除附件。

You can delete an attachment from PouchDB using the removeAttachment() method.

Syntax

下面是 removeAttachment() 方法的语法。对于此方法,您必须传递文档 ID、附件 ID 和 _rev 值。此方法还接受可选回调函数。

Following is the syntax of the removeAttachment() method. To this method, we have to pass the document id, attachment id, and _rev value. This method also accepts an optional callback function.

db.removeAttachment ( docId, attachmentId, rev, [callback] );

Example

假设 PouchDB 中存在 ID 为 001 的文档,该文档包含 ID、姓名、年龄、员工职位以及如下所示的附件。

Suppose there is a document in PouchDB with id 001, which contains id, name, age, designation of an employee along with an attachment as shown below.

{
   name: 'Raju',
   age: 23,
   designation: 'Designer',
   _attachments: {
      'att_1.txt': {
         content_type: 'text/plain',
         digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
         data: 'AA=='
      }
   },
   _id: '001',
   _rev: '2-cdec6c9f45ddbee7d456945654742d43'
}

下面是使用 removeAttachment() 方法从存储在 PouchDB 中的此文档 001 删除附件的示例。

Following is an example of deleting the attachment of this document 001 stored in PouchDB, using removeAttachment() method.

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

//Creating the database object
var db = new PouchDB('my');
db.removeAttachment('001', 'att_1.txt', '2-cdec6c9f45ddbee7d456945654742d43',
   function(err, res) {
   if (err) {
      return console.log(err);
   } else {
      console.log(res+"Attachment Deleted successfully")
   }
});

将上述代码另存为名为 Remove_Attachment.js 的文件。打开命令提示符,再使用 node 执行 JavaScript 文件,具体如下。

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

C:\PouchDB_Examples >node Remove_Attachment.js

此操作会移除文档的附件,并在控制台上显示消息,如下所示。

This removes the attachment of the document and displays a message on the console as shown below.

Attachment deleted successfully

删除后,通过执行以下代码验证文档内容。

After deletion, you can verify the contents of the document by executing the following code.

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

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

//Reading the Document
db.get('001',{attachments: true}, function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

将此代码另存为 read.js ,并执行该代码。执行后,您会得到附件删除后的文档内容,如下所示。

Save this code as read.js and execute it. On executing, you will get the contents of the document after deleting the attachment, as shown below.

{
   name: 'Raju',
   age: 23,
   designation: 'Designer',
   _id: '001',
   _rev: '3-da775487a6ed0495f2e49c543384f8e8'
}

Removing Attachment from a Remote Document

您还可以删除存储在服务器(CouchDB)远程的现有文档的附件。

You can delete an attachment of an existing document 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 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.

removing attachment from remote database

如果您选择名为 my_database 的数据库,则可以查看如下所示的内容。

And if you select the database named my_database, you can view its contents as shown below.

removing attachment database

假设在此文档中附有如下所示的附件。

Suppose there is an attachment in this document as shown below.

removing attachment

以下是一个示例,用于删除 001 中提到的、存在于名为 my_database 的数据库中的附件,该附件存储在 CouchDB 服务器中。

Following is an example of deleting the above mentioned attachment of the document 001 that exists 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');

db.removeAttachment('001', 'att_1.txt', '2-049f1c4ffa54576ec0947b65e34de423',
   function(err, res) {
   if (err) {
      return console.log(err);
   } else {
      console.log(res+"Attachment Deleted successfully")
   }
});

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

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

C:\PouchDB_Examples >node Remote_Delete_Attachment.js

这会删除现有附件并显示以下消息。

This removes the existing attachment and displays the following message.

Attachment Deleted successfully

如果您再次打开文档,您会发现附件已经被删除,如下面的屏幕截图中所示。

If you visit the document again, you can notice that the attachment was deleted as shown in the following screenshot.

attachment deleted