Pouchdb 简明教程

PouchDB - Quick Guide

PouchDB - Overview

本章简要介绍 PouchDB 及其功能和工作原理。

This chapter provides a brief introduction to PouchDB along with its features and how it works.

What is PouchDB?

PouchDB 是一种用 JavaScript 编写的开源 in-browser database API 。它以 Couch DB &minuss;一个 NoSQL 数据库为模型。使用此 API,我们可以构建可在线和离线工作的应用程序。它在内部使用 WebSQL 和 IndexedDB 来存储数据。

PouchDB is an open source in-browser database API written in JavaScript. It is modelled after Couch DB &minuss; a NoSQL database. Using this API, we can build applications that work offline and online. It internally uses WebSQL and IndexedDB to store data.

How Does it Work?

在 PouchDB 中,当应用程序处于离线状态时,数据会使用 WebSQL 和 IndexedDB 本地存储在浏览器中。当应用程序重新联机时,它会与 CouchDB 和兼容服务器同步。

In PouchDB, when the application is offline, the data is stored locally using WebSQL and IndexedDB in the browser. When the application is back online, it is synchronized with CouchDB and compatible servers.

使用 PouchDB 即可与本地和远程数据库进行无缝通信,无需注意到任何差异。

Using PouchDB, you can communicate with both local and remote databases seamlessly without noticing any difference.

Features of PouchDB

以下是 PouchDB 的功能:

Following are the features of PouchDB −

  1. Cross Browser − The API provided by PouchDB works the same in every environment, therefore, we can run a PouchDB application in various browsers.

  2. Light Weight − PouchDB is a very light-weight API, it is also included easily just using a script tag.

  3. Easy to Learn − If you have a prior knowledge of any programming language, it is easy to learn PouchDB.

  4. Open Source − PouchDB is an Open Source Application and is available on GitHub.

Advantages of PouchDB

以下是 PouchDB 的优势:

Following are the advantages of PouchDB −

  1. Since PouchDB resides inside the browser, there is no need to perform queries over the network, this results in faster execution of queries.

  2. You can synchronize the data with any of the supported server and by doing so you can run apps both online and offline.

Browsers that Support PouchDB

以下是支持 PouchDB 的浏览器:

Following are the browsers that support PouchDB −

  1. Firefox 29+ (Including Firefox OS and Firefox for Android)

  2. Chrome 30+

  3. Safari 5+

  4. Internet Explorer 10+

  5. Opera 21+

  6. Android 4.0+

  7. iOS 7.1+

  8. Windows Phone 8+

PouchDB - Environment

本章说明如何在系统中下载和安装PouchDB。

This chapter explains how to download and install PouchDB in your system.

Installing PouchDB

为了使用PouchDB,你需要下载文件 .js 并将其包含在你的脚本中。以下是安装PouchDB的步骤。

In order to work with PouchDB, you need to download the file .js file and include it in your script. Following are the steps to install PouchDB.

Step 1

通过单击以下链接访问PouchDB网站的主页 −

Visit the homepage of PouchDB website, by clicking the following link −

pouchdb homepage

Step 2

单击网页右上角的下载按钮,如上图所示。这将在你的系统中下载 PouchDB-5.3.0.min.js

Click the Download button on the top right hand side of the web page as shown in the above screenshot. This will download PouchDB-5.3.0.min.js in your system.

Step 3

PouchDB-5.3.0.min.js 复制并粘贴到你的工作目录,并将其包含在你的JavaScript中,如下面的命令所示。

Copy and paste the PouchDB-5.3.0.min.js to your working directory and include it in your JavaScript as shown in the following command.

<script src = "PouchDB-5.3.0.min.js"></script>

Installing Pouch Using Node.js

你还可以将PouchDB安装为Node.js模块。以下是使用Node.js安装PouchDB的步骤。

You can also install PouchDB as Node.js module. Following are the steps to install PouchDB using Node.js.

Step 1

按照我们 coffee script 教程的安装Node.js部分中提供的步骤安装Node.js。

Install Node.js by following the steps given in the Installing Node.js section of our coffee script tutorial.

Step 2

打开命令提示符并执行以下命令。这将在你的系统中安装PouchDB节点模块。

Open the command prompt and execute the following command. This will install PouchDB node module in your system.

npm install --save PouchDB

Downloading CouchDB

当离线时,PouchDB将数据存储在本地,并像应用程序一样工作。你可以通过连接到兼容的服务器在线访问它。众所周知,PouchDB可以连接到CouchDB,所以,我们也安装CouchDB。以下是安装CouchDB的步骤。

When offline, PouchDB stores data locally and works like an app. You can access it online by connecting with compatible servers. As we know PouchDB can be connected to CouchDB, so, lets install CouchDB too. Following are the steps to install CouchDB.

Step 1

CouchDB的官方网站为 http://couchdb.apache.org 。如果你单击给定的链接,你可以获得CouchDB官方网站的主页,如下面的截图所示。

The official website for CouchDB is http://couchdb.apache.org. If you click the given link, you can get the home page of CouchDB official website as shown in the following screenshot.

download couchdb

Step 2

如果你单击下载按钮,会转到一个页面,其中提供了CouchDB的各种格式的下载链接。以下快照说明了这一点。

If you click on the download button that will lead to a page where the download links of CouchDB in various formats are provided. The following snapshot illustrates the same.

couchdb download links formats

Step 3

选择Windows系统的下载链接,并选择一个提供的镜像来开始下载。

Choose the download link for Windows Systems and select one of the provided mirrors to start your download.

Installing CouchDB

一个Windows可执行 setup-couchdb-1.6.1_R16B02.exe 文件将下载到你的系统中。运行安装文件并继续安装。

A windows executable setup-couchdb-1.6.1_R16B02.exe file will be downloaded on your system. Run the setup file and proceed with the installation.

在成功安装CouchDB到你的系统后,打开安装CouchDB的文件夹,转到bin文件夹,并通过运行名为 couchdb.bat 的脚本文件来启动服务器。

After installing CouchDB in your system successfully, open the folder where CouchDB was installed, go to the bin folder, and start the server by running a script file named couchdb.bat.

安装完成后,访问以下链接打开 CouchDB 的内置 Web 界面: http://127.0.0.1:5984/ 。如果一切顺利,您将获得一个 Web 页面,其中包含以下输出。

After installation, open built-in web interface of CouchDB by visiting the following link − http://127.0.0.1:5984/. If everything goes fine, this will give you a web page, which will have the following output.

{
   "couchdb":"Welcome","uuid":"c8d48ac61bb497f4692b346e0f400d60",
   "version":"1. 6.1",
   "vendor": {
      "version":"1.6.1","name":"The Apache Software Foundation"
   }
}

您可以使用以下 URL 与 CouchDB Web 界面交互:

You can interact with CouchDB web interface by using the following URL −

http://127.0.0.1:5984/_utils/

这会向您展示 Futon 的索引页面,它是 CouchDB 的 Web 界面。

This shows you the index page of Futon, which is the web interface of CouchDB.

web interface

PouchDB - Create Database

你可以使用 PouchDB 构造函数在 PouchDB 中创建一个数据库。

You can create a database in PouchDB using the PouchDB constructor.

Syntax

以下是使用 PouchDB 构造函数的语法。你需要将数据库名称作为参数传递给它。

Following is the syntax of using the PouchDB constructor. To this, you need to pass the name of the database as a parameter.

new PouchDB(Database_name)

Example

要使用 node 在 PouchDB 中创建一个数据库,首先,你需要使用 require() 方法引用 PouchDB 包,然后你可以像以下示例中所示那样创建一个数据库。

To create a database in PouchDB using node, first of all, you need to require the PouchDB package using the require() method and then you can create a database as shown in the following example.

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

//Creating the database object
var db = new PouchDB('my_database');
console.log ("Database created Successfully.");

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

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

C:\PouchDB_Examples>node Create_Database.js

这将在本地创建一个数据库(你可以在当前目录中看到文件夹),显示以下消息。

This will create a database locally (you can see the folder in the current directory) displaying the following message.

Database created Successfully.

PouchDB - Database Info

你可以使用名为 info() 的方法获取关于数据库的基本信息。

You can get the basic information about the database using the method named info()

Syntax

以下是使用 PouchDB 的 info() 方法的语法。此方法接受回调函数。

Following is the syntax of using the info() method of PouchDB. This method accepts a callback function.

db.info([callback])

Example

以下是使用 info() 方法检索数据库信息的一个示例。在这里,我们显示了名为 my_database 的数据库的信息。如果发生错误,错误将显示在控制台上。

Following is an example of retrieving database information using the info() method. Here, we are displaying the information of the database named my_database. In case of error, the error will be displayed on the console.

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

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

//Database information
db.info(function(err, info) {
   if (err) {
      return console.log(err);
   } else {
      console.log(info);
   }
});

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

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

C:\PouchDB_Examples>node Database_info.js

这将显示指定数据库的信息,如下所示:

This will display the info of the specified database as follows.

{
   doc_count: 0,
   update_seq: 0,
   backend_adapter: 'LevelDOWN',
   db_name: 'my_database',
   auto_compaction: false,
   adapter: 'leveldb'
}

Remote Database Info

同样,你可以获取在服务器(CouchDB)上远程保存的数据库的信息。为此,你需要传递 CouchDB 中所需数据库的路径,而不是数据库名称。

In the same way, you get the information of a database that is saved remotely on the server (CouchDB). To do so, instead of database name, you need to pass the path to the required database in CouchDB.

Example

以下是一个获取存储在 CouchDB 服务器中的数据库信息的示例。此代码提供了名为 my_database 的数据库的信息。

Following is an example of retrieving information of a database that is saved in the CouchDB server. This code gives you information of a database named my_database.

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

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

//Database information
db.info(function(err, info) {
   if (err) {
      return console.log(err);
   } else {
      console.log(info);
   }
});

将上述代码保存在一个名为 Database_ Remote_info.js 的文件中。打开命令提示符并使用 node 执行 JavaScript 文件,如下所示:

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

C:\PouchDB_Examples>node Database_Remote_info.js

这将显示指定数据库的信息,如下所示:

This will display the info of the specified database as follows.

{
   db_name: 'my_database',
   doc_count: 0,
   doc_del_count: 0,
   update_seq: 0,
   purge_seq: 0,
   compact_running: false,
   disk_size: 79,
   data_size: 0,
   instance_start_time: '1458209191708486',
   disk_format_version: 6,
   committed_update_seq: 0,
   host: 'http://localhost:5984/my_database/',
   auto_compaction: false,
   adapter: 'http'
}

PouchDB - Delete Database

您可以使用 db.destroy() 方法删除 PouchDB 中的数据库。

You can delete a database in PouchDB using the db.destroy() method.

Syntax

以下为 db.destroy() 方法的语法。此方法接受一个回调函数作为参数。

Following is the syntax of using the db.destroy() method. This method accepts a callback function as a parameter.

db.destroy()

Example

以下是使用 destroy() 方法删除 PouchDB 中数据库的示例。在此,我们删除了名为 my_database 的数据库,已在前一章节中创建。

Following is an example of deleting a database in PouchDB using the destroy() method. Here, we are deleting the database named my_database, created in the previous chapters.

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

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

//deleting database
db.destroy(function (err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log ("Database Deleted”);
   }
});

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

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

C:\PouchDB_Examples >node Delete_Database.js

这会删除名为 my_database 并存储在本地数据库中的数据库,并显示以下消息。

This will delete the database named my_database which is stored locally displaying the following message.

Database Deleted

Deleting a Remote Database

同样的,您可以删除存储在服务器(CouchDB)远端上的数据库。

In the same way, you can delete a 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 that is required to be deleted, 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.

deleting remote database

以下是删除一个存储在 CouchDB 服务器中名为 my_database 的数据库的示例。

Following is an example of deleting 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');

//deleting database
db.destroy(function (err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Database Deleted");
   }
});

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

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

C:\PouchDB_Examples >Remote_Database_Delete.js

这会从 PouchDB 中删除指定数据库,并显示以下消息。

This deletes the specified database from PouchDB displaying the following message.

Database Deleted

Verification

执行上述程序后,如果您再次打开 URL,您会看到以下截图。在此,您可以看到只有两个数据库,因为 my_database 已被删除。

After executing the above program, if you visit the URL again, you will get the following screenshot. Here you can observe only two databases since my_database was deleted.

delete database verification

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

PouchDB - Read Document

您可以使用 db.get() 方法读取/获取 PouchDB 中文档的内容。

You can read/retrieve the contents of a document in PouchDB using the db.get() method.

Syntax

以下是 PouchDB 的 db.get() 方法的语法。此方法接受 document id 和可选回调函数。

Following is the syntax of using the db.get() method of PouchDB. This method accepts the document id and an optional callback function.

db.get(document, callback)

Example

以下是如何使用 get() 方法读取 PouchDB 中文档内容的示例。

Following is an example of reading the contents of a document in PouchDB using the get() method.

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

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

//Reading the contents of a Document
db.get('001', function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

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

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

C:\PouchDB_Examples >node Read_Document.js

这会读取存储在本地名为 my_database 的数据库中的给定文档的内容。控制台中将会显示以下消息。

This reads the contents of the given document that exists in the database named my_database which is stored locally. The following message gets displayed on the console.

{
   name: 'Raju',
   age: 23,
   designation: 'Designer',
   _id: '001',
   _rev: '1-ba7f6914ac80098e6f63d2bfb0391637'
}

Reading a Document from a Remote Database

您还可以从服务器上远程存储在数据库(CouchDB)中读取文档。

You can also read a document 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.

reading document from remote database

通过点击名为 my_database 的数据库,你可以看到以下截图。这里,你可以观察到这个数据库包含一个编号为 001 的文档。

By clicking on the database named my_database you can see the following screenshot. Here, you can observe that this database contains a document with id 001.

reading document

以下是读取编号为“ 001 ”的文档内容的一个示例,该文档存在于名为 my_database 的数据库中,该数据库存储在CouchDB服务器中。

Following is an example of reading the contents of the document having id as “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');

//Reading the contents of a document
db.get('001', function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

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

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

C:\PouchDB_Examples >node Remote_Read_Document.js

这将读取存在于名为 my_database 的数据库中的给定文档的内容,该数据库存储在CouchDB中。以下消息将显示在控制台上。

This reads the contents of the given document that exists in the database named my_database which is stored in CouchDB. The following message is displayed on the console.

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

PouchDB - Update Document

无论何时在 PouchDB 中创建文档,都会生成一个新字段 _rev ,它被称为 revision marker 。每次对文档进行更改时, _rev 的值都是一个唯一的随机数, _rev 的值都会发生更改。

Whenever, we create a document in PouchDB, a new field _rev is generated, and it is known as revision marker. The _rev‘s value is a unique random number, each time we make changes to the document the value of _rev is changed.

你可以使用 (_rev) 更新 PouchDB 中的现有文档。为此,首先检索我们想要更新的文档的 _rev 值。现在,将要更新的内容与检索的 _rev 值一起放入新文档,最后使用 put() 方法将此文档插入 PouchDB。

You can update an existing document in PouchDB using the (_rev). To do so, first of all retrieve the _rev value of the document we want to update. Now, place the contents that are to be updated along with the retrieved _rev value in a new document, and finally insert this document in PouchDB using the put() method.

Example

假设我们在 PouchDB 中有一个 id 为 001 的文档,其中包含一个人的详细信息。为了更新此文档,我们应该有它的 rev 编号。因此,要检索文档的内容,可以使用以下代码。

Assume we have a document in PouchDB with id 001 which has details of a person. In order to update this document, we should have its rev number. Therefore, to retrieve the contents of the document the following code is used.

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

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

//Reading the contents of a Document
db.get('001', function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

执行上述代码,你将收到以下输出。

On executing the above code, you will receive the following output.

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

现在,使用 _rev ,你可以将键 “ age ” 的值更新为 26,如以下代码所示。

Now, using the _rev you can update the value of the key “age” to 26, as shown in the following code.

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

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

//Preparing the document for update
doc = {
   age: 26,
   _rev: '3-552920d1ca372986fad7b996ce365f5d',
   }

//Inserting Document
db.put(doc);

//Reading the contents of a Document
db.get('001', function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

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

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

C:\Pouch_Examples>node Update_Document.js

这将更新存储在名为 my_database 的数据库中的给定文档的内容。数据库以本地形式存储。控制台上将显示以下消息。

This updates the contents of the given document that exists in the database named my_database which is stored locally. The following message is displayed on the console.

{
   name: 'Raju',
   age: 26,
   designation: 'Designer',
   _id: '001',
   _rev: '2-61b523ccdc4e41a8435bdffbb057a7a5'
}

Updating a Document in a Remote Database

你还可以更新一个存储在服务器(CouchDB)上的远程数据库中的现有文档。

You can also update an existing document in a 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 updated.

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.

updating document in remote database

单击名为 my_database 的数据库,你可以看到以下屏幕截图。在这里,你可以观察到此数据库包含一个 ID 为 001 的文档。

By clicking on the database named my_database, you can see the following screenshot. Here, you can observe that this database contains a document with id 001.

updating  database

以下是更新存储在名为 my_database ,CouchDB 服务器中存储的数据库中的 ID 为 “ 001 ” 的文档的年龄的示例。

Following is an example of updating the age of the document having id as “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');

//Preparing the document for update
doc = {
   age: 26,
   _rev: '3-552920d1ca372986fad7b996ce365f5d',
   }

//Inserting Document
db.put(doc);

//Reading the contents of a Document
db.get('001', function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

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

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

C:\PouchDB_Examples >node Remote_Update_Document.js

这将更新存储在名为 my_database 的 CouchDB 中的数据库中的给定文档的内容。控制台上将显示以下消息。

This updates the contents of the given document that exists in the database named my_database which is stored in CouchDB. The following message is displayed on the console.

{
   _id: '001',
   _rev: '2-b9640bffbce582c94308905eed8bb545',
   name: 'Raju',
   age: 26,
   designation: 'Designer'
}

PouchDB - Delete Document

你可以使用 db.remove() 方法从 PouchDB 中存在的数据库中删除文档。

You can delete a document from a database that exists in PouchDB using the db.remove() method.

Syntax

以下是使用 PouchDB 的 db.remove() 方法的语法。对于此方法,我们必须传递 id_rev 来删除现有文档,如下面的代码所示。此方法接受可选的回调函数。我们还可以传递完整文档,而不是 id 和 _rev。

Following is the syntax of using the db.remove() method of PouchDB. To this method, we have to pass id and _rev to delete an existing document as shown in the following code. This method accepts an optional callback function. We can also pass the complete document instead of id and _rev.

db. get ( docId, docRev, [callback] )
or
db. get ( docId, docRev, [callback] )

Example

假设我们在 PouchDB 中有一个 id 为 001 的文档,其中包含一个人的详细信息。为了删除这个文档及其 id ,我们还应该有其 _rev 编号。因此,请按照以下代码所示检索文档的内容。

Assume we have a document in PouchDB with id 001 which have the details of a person. In order to delete this document along with its id we should also have its _rev number. Therefore, retrieve the contents of the document as shown in the following code.

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

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

//Reading the contents of a Document
db.get('001', function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

执行上述代码会得到以下输出。

Executing the above code gives the following output.

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

现在,您可以使用前面记录的 _rev 和文档的 ID,再借助 remove() 方法来删除文档,具体如以下代码所示。

Now, using the _rev and id of the document you can delete this by using the remove() method as shown in the following code.

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

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

//Deleting an existing document
db.remove('001', '3-552920d1ca372986fad7b996ce365f5d', function(err) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Document deleted successfully");
   }
});

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

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

C:\PouchDB_Examples >node Delete_Document.js

此操作会删除本地存储的,名为 my_database 数据库中的指定文档的内容。还会显示以下消息。

This deletes the contents of the given document that exists in the database named my_database which is stored locally. The following message is displayed.

Document deleted successfully

Deleting a Document from a Remote Database

您还可以从服务器(CouchDB)远程存储的数据库中删除现有文档。

You can also delete an existing document 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 document from remote database

通过单击名为 my_database 的数据库,您可以看到以下屏幕截图。此处您可以看到数据库中包含 ID 为 001 的文档。

By clicking on the database named my_database you can see the following screenshot. Here, you can observe that the database contains a document with id 001.

deleting database

以下是一个示例:删除存储在 CouchDB 服务器上的,名为 my_database 的数据库中,ID 为“ 001 ”的文档的内容。

Following is an example of deleting the contents of the document having id “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');

//Deleting an existing document
db.remove('001', '3-552920d1ca372986fad7b996ce365f5d', function(err) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Document deleted successfully");
   }
});

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

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

C:\PouchDB_Examples >node Remote_Delete_Document.js

此操作会删除存储在 CouchDB 中的,名为 my_database 数据库中的指定文档。还会显示以下消息。

This deletes the given document that exists in the database named my_database which is stored in CouchDB. The following message is displayed.

Document deleted successfully

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

PouchDB - Fetch Batch

你可以使用 allDocs() 方法从 PouchDB 中的一个数据库读取/检索多个/批量文档。

You can read/retrieve multiple/bulk documents from a database in PouchDB using the allDocs() method.

Syntax

以下是使用 PouchDB 的 db.allDocs() 方法的语法。此方法接受一个可选的回调函数。

Following is the syntax of using the db.allDocs() method of PouchDB. This method accepts an optional callback function.

db.allDocs()

Example

以下是使用 db.allDocs() 方法检索存储在名为 my_database 的本地数据库中的所有文档的示例。此方法以对象的形式检索文档数组,要获取每个文档的内容,你需要调用为 docs.rows

Following is an example of retrieving all the documents in a database named my_database that is stored locally, using db.allDocs() method. This method retrieves the array of documents in the form of objects, to get the contents of each document you need to call as docs.rows.

//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(function(err, docs) {
   if (err) {
      return console.log(err);
   } else {
      console.log (docs.rows);
   }
});

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

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

C:\PouchDB_Examples >node Read_All_Document.js

这将读取存储在名为 my_database 的本地数据库中的所有文档。控制台上将显示以下消息。

This reads all the documents that exists in the database named my_database which is stored locally. The following message is displayed on the console.

[
   {
      id: '001',
      key: '001',
      value: { rev: '1-9dc57f5faa7ea90eeec22eba8bfd05f5' }
   },
   {
      id: '002',
      key: '002',
      value: { rev: '1-9bf80afcedb9f8b5b35567292affb254' }
    },
   {
      id: '003',
      key: '003',
      value: { rev: '1-1204f108e41bf8baf867856d5da16c57' }
   }
]

通常,如上述结果所示,使用 allDocs() 方法,你只能看到每个文档的 _id, key_rev 字段。但是,要将整个文档包含在结果中,你必须将可选参数 include_docs 设置为 true,如下所示。

In general, as shown in the above result, using allDocs() method you can see only the _id, key and _rev fields of each document. However, to include the whole document in the result, you have to make the optional parameter include_docs true as shown below.

//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);
   }
});

执行上述代码将为你提供指定文档中的完整文档列表,如以下代码所示。

Executing the above code gives you a list of complete documents in the specified documents as shown in the following code.

[
   {
      id: '001',
      key: '001',
      value: { rev: '1-9dc57f5faa7ea90eeec22eba8bfd05f5' },
      doc: {
         name: 'Ram',
         age: 23,
         Designation: 'Programmer',
         _id: '001',
         _rev: '1-9dc57f5faa7ea90eeec22eba8bfd05f5'
      }
   },
   {
      id: '002',
      key: '002',
      value: { rev: '1-9bf80afcedb9f8b5b35567292affb254' },
      doc: {
         name: 'Robert',
         age: 24,
         Designation: 'Programmer',
         _id: '002',
         _rev: '1-9bf80afcedb9f8b5b35567292affb254'
      }
   },
   {
      id: '003',
      key: '003',
      value: { rev: '1-1204f108e41bf8baf867856d5da16c57' },
      doc: {
         name: 'Rahim',
         age: 25,
         Designation: 'Programmer',
         _id: '003',
         _rev: '1-1204f108e41bf8baf867856d5da16c57'
      }
   }
]

Reading a Batch from a Remote Database

你还可以从存储在服务器(CouchDB)上的远程数据库中获取所有文档。

You can also fetch 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.

reading batch from remote database

以下是如何阅读存储在 CouchDB 服务器中的名为 my_database 的数据库中的所有文档的示例。

Following is an example of reading 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');

//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);
   }
});

将上述代码另存为 Remote_Read_AllDocument.js 文件名。打开命令提示符,然后使用 node 执行 JavaScript 文件,如下所示。

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

C:\PouchDB_Examples >node Remote_Read_AllDocument.js

这将读取存储在 CouchDB 中的、名为 my_database 的数据库中给定文档的内容,并在控制台上进行如下所示的显示。

This reads the contents of the given document that exists in the database named my_database which is stored in CouchDB, and displays on the console as shown below.

[
   {
      id: '001',
      key: '001',
      value: { rev: '3-552920d1ca372986fad7b996ce365f5d' },
      doc: {
         _id: '001',
         _rev: '3-552920d1ca372986fad7b996ce365f5d',
         name: 'Raju',
         age: 23,
         designation: 'Designer'
      }
   },
   {
      id: '002',
      key: '002',
      value: { rev: '1-9af15cb11054ebe03a7816bf6c5e4128' },
      doc: {
         _id: '002',
         _rev: '1-9af15cb11054ebe03a7816bf6c5e4128',
         name: 'Robert',
         age: 24,
         Designation: 'Programmer'
      }
   },
   {
      id: '003',
      key: '003',
      value: { rev: '1-3033b5a78e915c52fd37325d42eb3935' },
      doc: {
         _id: '003',
         _rev: '1-3033b5a78e915c52fd37325d42eb3935',
         name: 'Rahim',
         age: 25,
         Designation: 'Programmer'
      }
   }
]

PouchDB - Update Batch

你可以使用 bulkDocs() 方法一次更新 PouchDB 中的文档阵列。为此,你需要创建包含这些文档的阵列,每个文档包括 _id, _rev 和需更新的值。

You can update an array of documents in PouchDB at once using the bulkDocs() method. To do so you need to create an array of documents where, each document contains _id, _rev and the values that are to be updated.

假设存储在 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'}

假设我们要将所有 3 个文档中的年龄值增加 2 岁。为此,首先你需要获取 _rev 值。因此,使用以下代码提取这些文档的内容。

Suppose we have to increase the age values in all the 3 documents by 2 years. For this to happen, first you need to get the _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. On 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 update 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_databas');

//Preparing the document
docs = [{_id : '001', _rev: '1-1604b0c3ff69dc1e261265fd60808404', age : 25, },
      {_id : '002', _rev: '1-b5e49db7e984841bf12a13e3ee548125', age : 26, },
      {_id : '003', _rev: '1-a7b342786ecc707aa91f3b321a177b51', age : 27 }]

//Updating the documents in bulk
db.bulkDocs(docs, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Documents Updated Successfully");
   }
});

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

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

C:\PouchDB_Examples >node Update_All_Document.js

这会更新存储在本地名为 my_database 的数据库中存在的所有文档,显示以下消息。

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

Documents Updated Successfully

现在,如果你在回调之前向 allDocs() 函数添加 {include_docs: true} 作为参数执行 bulk_fetch.js 程序,那么你将可以看到已更新的文档值,如下所示。

Now, if you execute the bulk_fetch.js program by adding {include_docs: true} as a parameter to allDocs() function, before the callback, then, you will can see the values of the documents updated, as shown below.

[
   {
      id: '001',
      key: '001',
      value: { rev: '2-77f3a9974dd578d12f3f2a33aae64c8d' },
      doc: {
         age: 25,
         _id: '001',
         _rev: '2-77f3a9974dd578d12f3f2a33aae64c8d'
      }
   },
   {
      id: '002',
      key: '002',
      value: { rev: '2-43966007568ce9567c96422195fcfa0d' },
      doc: {
         age: 26,
         _id: '002',
         _rev: '2-43966007568ce9567c96422195fcfa0d'
      }
   },
   {
      id: '003',
      key: '003',
      value: { rev: '2-6c5349652527f4f39583ff14f23cd677' },
      doc: {
         age: 27,
         _id: '003',
         _rev: '2-6c5349652527f4f39583ff14f23cd677'
      }
   }
]

Updating 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.

updating batch from remote database

假设我们选择名为 my_database 的数据库,你可以观察到它包含 3 个文档,如下所示。

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

updating batch

现在,使用以下代码获取这些文档的内容。

Now, 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('http://localhost:5984/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);
   }
});

将上述代码保存为 remote_bulk_fetch.js 。执行后,上述程序会显示数据库中所有文档的内容,如下所示。

Save the above code as remote_bulk_fetch.js. On executing, the above program gives you the contents of all the documents in the database as shown below.

[
   {
      id: '001',
      key: '001',
      value: { rev: '3-552920d1ca372986fad7b996ce365f5d' },
      doc: {
         _id: '001',
         _rev: '3-552920d1ca372986fad7b996ce365f5d',
         name: 'Raju',
         age: 23,
         designation: 'Designer'
      }
   },
   {
      id: '002',
      key: '002',
      value: { rev: '1-9af15cb11054ebe03a7816bf6c5e4128' },
      doc: {
         _id: '002',
         _rev: '1-9af15cb11054ebe03a7816bf6c5e4128',
         name: 'Robert',
         age: 24,
         Designation: 'Programmer'
      }
   },
   {
      id: '003',
      key: '003',
      value: { rev: '1-3033b5a78e915c52fd37325d42eb3935' },
      doc: {
         _id: '003',
         _rev: '1-3033b5a78e915c52fd37325d42eb3935',
         name: 'Rahim',
         age: 25,
         Designation: 'Programmer'
      }
   }
]

以下是在存储于 CouchDB 服务器的名为 my_database 的数据库中更新所有存在的文档的示例。

Following is an example of updating all the documents 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');

//Preparing the document
docs = [{_id : '001', _rev: '3-552920d1ca372986fad7b996ce365f5d', age : 24, },
      {_id : '002', _rev: '1-9af15cb11054ebe03a7816bf6c5e4128', age : 26, },
      {_id : '003', _rev: '1-3033b5a78e915c52fd37325d42eb3935', age : 27}]

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

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

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

C:\PouchDB_Examples >node Remote_Update_Document.js

这会更新存储在 CouchDB 中存在于名为 my_database 的数据库中的所有给定文档的内容,并显示以下消息。

This updates 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 Updated Successfully

现在,如果你执行 remote_bulk_fetch.js 程序,你将可以看到已更新的文档值,如下所示。

Now, if you execute the remote_bulk_fetch.js program you will can see the values of the documents updated, as shown below.

[
   {
      id: '001',
      key: '001',
      value: { rev: '4-6bc8d9c7a60fed2ed1667ec0740c1f39' },
      doc: {
         _id: '001',
         _rev: '4-6bc8d9c7a60fed2ed1667ec0740c1f39',
         age: 25
      }
   },
   {
      id: '002',
      key: '002',
      value: { rev: '2-1aa24ce77d96bb9d2a0675cdf1e113e0' },
      doc: {
         _id: '002',
         _rev: '2-1aa24ce77d96bb9d2a0675cdf1e113e0',
         age: 26
      }
   },
   {
      id: '003',
      key: '003',
      value: { rev: '2-fa113149ba618eda77f73072974a2bc1' },
      doc: {
         _id: '003',
         _rev: '2-fa113149ba618eda77f73072974a2bc1',
         age: 27
      }
   }
]

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

PouchDB - Adding Attachment

可以使用 PouchDB 中的 putAttachment() 方法将二进制对象附加到文档。

You can attach a binary object to a document using the putAttachment() method in PouchDB.

Syntax

以下是如何 putAttachment() 。必须将文档 ID、附件 ID、MIME 类型以及附件传递给此方法。此方法还接受可选的回调函数。

Following is the syntax of the putAttachment(). To this method, we have to pass the document id, attachment id, MIME type along with the attachment. This method also accepts an optional callback function.

db.putAttachment( docId, attachmentId, attachment, type, [callback] );

我们可用 blob 或 buffer 对象准备附件,其中 blob 用在使用浏览器时,而 buffer 用在与 Node.js 合作时,由于我们在 Node.js 中演示程序,我们使用 buffer 对象来准备文档。

We can prepare attachment using blob or buffer objects, where blob is used while working with the browser and buffer is used while working with Node.js, since we are demonstrating our programs in Node.js, we use buffer objects to prepare documents.

Example

以下是如何在 PouchDB 中使用 putAttachment() 方法,在名为 my_database 的数据库中创建一个含附件的文档。

Following is an example of creating a document with an attachment, within a database named my_database in PouchDB using putAttachment() method.

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

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

//Preparing the attachment
var my_attachment = new Buffer(['Welcome to tutorialspoint'], {type: 'text/plain'});

//Adding attachment to a document
db.putAttachment('001', 'att_1.txt', my_attachment, 'text/plain', function(err, res) {
   if (err) {
      return console.log(err);
   } else {
      console.log(res+"Attachment added successfully")
   }
});

将上述代码另存为 Add_Attachment.js 文件名。打开命令提示符,然后使用 node 执行 JavaScript 文件,如下所示。

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

C:\PouchDB_Examples >node Add_Attachment.js

这将创建一个空文档,并将附件添加到存储在 PouchDB 中的、名为 my_database 的数据库中,并显示以下消息。

This creates an empty document adding an attachment to it, in the database named my_database which is stored in PouchDB, and displays the following message.

Attachment added successfully

可以使用以下代码读取文档,以验证是否添加了附件。

You can verify whether the attachment is added by reading the document using the following code.

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

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

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

将上述代码另存为 read_doc.js 并执行它。执行此程序,可以看到以下文档内容。

Save the above code as read_doc.js and execute it. Executing this program, you can see the following contents of the document.

{
   _attachments: {
      att_1.txt: {
         content_type: 'text/plain',
         digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
         data: 'AA=='
      }
   },
   _id: '001',
   _rev: '1-620fd5f41d3328fcbf9ce7504338a51d'
}

Adding Attachment to an Existing Document

假设在 PouchDB 的 my_database 数据库中有一个文档,其 ID 为“ 002 ”。可以通过将 ID 值更改为 002 来执行 read_doc.js ,以获取其内容,如下所示。

Suppose, there is a document in a database by the name my_database PouchDB with id ‘002’. You can get the contents of it by executing the read_doc.js by changing the id value to 002, as shown below.

{
   name: 'Raju',
   age: 23,
   designation: 'Designer',
   _id: '002',
   _rev: '1-05ca7b5f3f4762a9fb2d119cd34c8d40'
}

现在,可以使用其 _rev 值将附件添加此文档。

Now, you can add an attachment to this document using its _rev value.

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

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

//Adding attachment to existing document
var my_attachment = new Buffer (['Welcome to tutorialspoint'], {type: 'text/plain'});

rev = '1-05ca7b5f3f4762a9fb2d119cd34c8d40';
db.putAttachment('002', 'att_1.txt', rev, my_attachment, 'text/plain', function(err, res) {
   if (err) {
      return console.log(err);
   } else {
      console.log (res + "Attachment added successfully")
   }
});

将上述代码另存为 Add_Attachment_to_doc.js 文件名。打开命令提示符,然后使用 node 执行 JavaScript 文件,如下所示。

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

C:\PouchDB_Examples >node Add_Attachment_to_doc.js

这将向指定文档添加附件,并显示以下消息。

This adds an attachment to the specified document displaying the following message.

Attachment added successfully

如果将 read_doc.js 中的 ID 值更改为 002 并执行它,则会得到以下输出。

If you change the id value in read_doc.js to 002 and execute it, you will get the following output.

{
   name: 'Raju',
   age: 23,
   designation: 'Designer',
   _attachments: {
      att_1: {
         content_type: 'text/plain',
         digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
         data: 'AA=='
      }
   },
   _id: '002',
   _rev: '2-3bb4891b954699bce28346723cc7a709'
}

Adding Attachment to a Remote Document

你甚至可以添加到存储在服务器 (CouchDB) 上的远程数据库中的文档中添加附件。

You can even add an attachment to the document existing in a 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.

adding attachment to remote database

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

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

adding attachment

以下是如何向存储在名为 my_database 的数据库中的文档 001 添加附件的示例(该数据库存储在 CouchDB 服务器中)。

Following is an example of adding an attachment to the document 001 stored 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');

//Adding attachment to existing document
var my_attachment = new Buffer (['Welcome to tutorialspoint'], {type: 'text/plain'});

rev = '1-36c34fdcf29a652876219065f9681602';
db.putAttachment('001', 'att_1.txt',rev, my_attachment, 'text/plain', function(err, res) {
   if (err) {
      return console.log(err);
   } else {
      console.log (res+ "Attachment added successfully")
   }
});

将上述代码另存为 Remote_Add_Attachment.js 文件名。打开命令提示符,然后使用 node 执行 JavaScript 文件,如下所示。

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

C:\PouchDB_Examples >node Remote_Add_Attachment.js

这将向指定文档添加附件,并显示以下消息。

This adds an attachment to the specified document displaying the following message.

Attachment added successfully

现在,如果你验证该文档,你会发现如以下屏幕截图所示,附件已添加到该文档。

Now, if you verify the document, you can observe the attachment added to it as shown in the following screenshot.

adding attachment verification

PouchDB - Retrieving Attachment

你可以使用 getAttachment() 方法从 PouchDB 检索附件。此方法永远返回 blob 或缓冲区对象。

You can retrieve an attachment from PouchDB using the getAttachment() method. This method always returns blob or buffer objects.

Syntax

以下是 getAttachment() 的语法。我们必须向此方法传递文档 ID 和附件 ID。此方法还接受可选的回调函数。

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

db.getAttachment( docId, attachmentId, [callback] );

Example

以下是使用 getAttachment() 方法检索存储在 PouchDB 中的文档附件的示例。使用此代码,我们试图从文档 001 检索附件 att_1.txt

Following is an example of retrieving an attachment of a document stored in PouchDB, using getAttachment() method. Using this code, we are trying to retrieve an attachment att_1.txt from the document 001.

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

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

//Retrieving an attachment from a document
db.getAttachment('001', 'att_1.txt', function(err, blob_buffer) {
   if (err) {
      return console.log(err);
   } else {
      console.log(blob_buffer);
   }
});

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

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

C:\PouchDB_Examples >node Retrieve_Attachment.js

这会检索文档的附件,并如下所示显示在控制台中。

This retrieves the attachment of the document and displays on the console as shown below.

<Buffer 00>

Retrieving Attachment from a Remote Document

你还可以检索存在于存储在服务器上的(CouchDB)数据库中的文档附件。

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

retrieving attachment from remote database

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

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

retrieving attachment

假设此文档中有一个附件,如下所示。

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

attachment

以下是针对存储在名为 my_database 的数据库中的文档 001 检索附件的示例,此数据库存储在 CouchDB 服务器中。

Following is an example of retrieving an 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');

//Retrieving an attachment from a document
db.getAttachment('001', 'att_1.txt', function(err, blob_buffer) {
   if (err) {
      return console.log(err);
   } else {
      console.log(blob_buffer);
   }
});

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

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

C:\PouchDB_Examples >node Remote_Retrieve_Attachment.js

这会检索文档附件,并如下所示显示在控制台中。

This retrieves the document attachment and displays it on the console as shown below.

<Buffer 00>

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

PouchDB - Replication

PouchDB 最重要的功能之一是复制,即你可以制作数据库副本。你可以复制存储在本地位置的 PouchDB 实例或存储在远程位置的 CouchDB 实例。

One of the most important features of PouchDB is replication, i.e. you can make a copy of a database. You can replicate either a PouchDB instance stored locally or a CouchDB instance stored remotely.

Syntax

以下是复制 PouchDB 中数据库的语法。此处, source database 的副本是目标。你可以直接以字符串格式向此方法传递源数据库和目标数据库的位置,或可以传递表示它们的项目。

Following is the syntax of replicating a database in PouchDB. Here, a copy of the source database is the target. To this method, you can directly pass the location of source and destination databases in String format, or you can pass objects representing them.

PouchDB.replicate(source, target, [options])

源和目标都可以是 PouchDB 实例或 CouchDB 实例。

Both the source and targets can be either PouchDB instances or CouchDB instances.

Replicating LocalDB to CouchDB

假设 PouchDB 中有一个名为 sample_database 的数据库,它包含 3 个文档 doc1、doc2 和 doc3,其内容如下所示。

Suppose there is a database with the name sample_database in PouchDB, and it contains 3 documents doc1, doc2, and doc3, having contents as shown below.

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

这是一个创建存储在 CouchDB 中名为 sample_database 的数据库副本的示例。

Following is an example which makes a copy of the database named sample_database that is stored locally in CouchDB.

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

var localdb = 'sample_database';

//Creating remote database object
var remotedb = 'http://localhost:5984/sample_database';

//Replicating a local database to Remote
PouchDB.replicate(localDB, remoteDB);
console.log ("Database replicated successfully");

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

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

C:\PouchDB_Examples >node Replication_example.js

这会创建 CouchDB 实例中名为 sample_database 的数据库的副本,并在控制台中显示一条消息,如下所示。

This makes a copy of the database named sample_database in CouchDB instance and displays a message on the console as shown below.

Database replicated successfully

通过点击以下链接,您可以验证数据库在您的 CouchDB 实例中是否已复制: http://127.0.0.1:5984/_utils/index.html

You can verify whether the database is replicated in your CouchDB instance by clicking the following link http://127.0.0.1:5984/_utils/index.html.

单击时,您会看到 CouchDB 中的数据库列表。您还可观察到一个数据库副本 sample_database 创建在此处。

On clicking, you can see the list of databases in your CouchDB. You can also observe that a copy of the database sample_database is created here.

sample database

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

If you select the replicated database, you can view its contents as shown below.

replicated database

Replicating CouchDB to PouchDB

假定在 CouchDB 中有一个名为 Remote_Database 的数据库,其中包含文档 doc1、doc2 和 doc3,内容如下所示。

Suppose there is a database with the name Remote_Database in CouchDB and it contains 3 documents, doc1, doc2, and doc3, having contents as shown below.

doc1 = {_id: '001', name: 'Geeta', age: 25, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Zara Ali', age: 24, Designation: 'Manager'}
doc3 = {_id: '003', name: 'Mary', age: 23, Designation: 'Admin'}

这是一个将 CouchDB 中名为 Remote_Database 的数据库的副本保存在本地存储中的示例。

Following is an example which makes a copy of the database named Remote_Database that is stored in CouchDB in the local storage.

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

var localdb = 'sample_database';

var remotedb = 'http://localhost:5984/sample_database1';

//Replicating a local database to Remote
PouchDB.replicate(remotedb, localdb);
console.log("Database replicated successfully");

使用 Replication_example2.js 将上述代码保存到文件中。打开命令窗口,并使用 node 执行 JavaScript 文件,如下所示。

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

C:\PouchDB_Examples >node Replication_example2.js

这将对 PouchDB 实例中名为 remote_database 的数据库进行副本,并在控制台上显示如下所示的消息。

This makes a copy of the database named remote_database in PouchDB instance and displays a message on the console as shown below.

Database replicated successfully

您可以通过执行以下代码来验证数据库是否在您的 Pouch 实例中复制。

You can verify whether the database is replicated in your Pouch instance by executing the following code.

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

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

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

如果数据库在执行上述代码后复制,您会得到复制的数据库内容,如下所示。

If the database is replicated on executing the above code, you will get the contents of the replicated database as shown below.

[
   {
      id: '001',
      key: '001',
      value: { rev: '1-23cf3767e32a682c247053b16caecedb' },
      doc: {
         name: 'Geeta',
         age: 25,
         Designation: 'Programmer',
         _id: '001',
         _rev: '1-23cf3767e32a682c247053b16caecedb'
      }
   },
   {
      id: '002',
      key: '002',
      value: { rev: '1-d5bcfafbd4d4fae92fd7fc4fdcaa3a79' },
      doc: {
         name: 'Zara Ali',
         age: 24,
         Designation: 'Manager',
         _id: '002',
         _rev: '1-d5bcfafbd4d4fae92fd7fc4fdcaa3a79'
      }
   },
   {
      id: '003',
      key: '003',
      value: { rev: '1-c4cce025dbd30d21e40882d41842d5a4' },
      doc: {
         name: 'Mary',
         age: 23,
         Designation: 'Admin',
         _id: '003',
         _rev: '1-c4cce025dbd30d21e40882d41842d5a4'
      }
   }
]

PouchDB - Synchronization

您可以将存储在 PouchDB 中的本地数据库与存储在 CouchDB 中的数据库同步。在上一章中,我们介绍了如何使用 PouchDB 复制数据库。我们在那里使用了 PouchDB.replicate(source, destination) 方法。

You can synchronize the databases stored locally in PouchDB with those that are stored in CouchDB. In the previous chapter, we have seen how to replicate databases using PouchDB. There we have used the method PouchDB.replicate(source, destination).

此外,我们还可以使用 replicate.to()replicate.from() 方法,将数据从本地数据库复制到远程数据库,并将数据从远程数据库复制到本地数据库,如下所示。

In addition to this, we can also replicate the data, from the local database to the remote database, and from the remote database to the local database using replicate.to() and replicate.from() methods as shown below.

//Replicating data from local database to remote database
localDB.replicate.to(remoteDB);

//Replicating data from remote database to local database
localDB.replicate.from(remoteDB);

其中, localDB 是存储在 PouchDB 中的数据库对象, remoteDB 是存储在 CouchDB 中的数据库对象。

Where, localDB is an object of database stored locally in PouchDB and remoteDB is an object of a database that is stored in CouchDB.

Example

假设 PouchDB 中有一个名为 local_database 的数据库,它包含 3 篇文档,doc1、doc2 和 doc3,其内容如下所示。

Suppose there is a database with the name local_database in PouchDB, and it contains 3 documents, doc1, doc2, and doc3, having contents as shown below.

doc1 = {_id: '003', name: 'Ram', age: 26, Designation: 'Programmer'}
doc2 = {_id: '004', name: 'Robert', age: 27, Designation: 'Programmer'}
doc3 = {_id: '005', name: 'Rahim', age: 28, Designation: 'Programmer'}

CouchDB 中有一个名为 Remote_Database 的数据库,其中包含 2 个文档 doc1 和 doc2,内容如下所示。

And there is a database with the name Remote_Database in CouchDB and it contains 2 documents doc1, doc2, having contents as shown below.

doc1 = {_id: '001', name: 'Geeta', age: 25, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Zara Ali', age: 24, Designation: 'Manager'}

接下来是一个同步这两个数据库的示例,其中一个存储在 PouchDB 中,另一个存储在 CouchDB 中,使用了 replicate.to()replicate.from() 方法。

Following is an example of synchronizing these two databases, where one is stored in PouchDB and other is stored in CouchDB, using the replicate.to() and replicate.from() methods.

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

//Creating local database object
var localDB = new PouchDB('local_database');

//Creating remote database object
var remoteDB = new PouchDB('http://localhost:5984/remote_database');

//Synchronising both databases
localDB.replicate.to(remoteDB);
remoteDB.replicate.from(localDB);
console.log("Databases synchronized successfully");

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

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

C:\PouchDB_Examples >node Synchronising_databases.js

这将同步这两个数据库 remoteDB 和 localDB,并在控制台上显示一条消息,如下所示:

This synchronizes the two databases remoteDB and localDB, and displays a message on the console as shown below.

Databases synchronized successfully.

同步这两个数据库后,访问 http://127.0.0.1:5984/_utils/index.html 并选择 remote_database 。你可以观察到,本地数据库的文档(003、004、005)被复制到了这个数据库中,如下所示:

After synchronizing the two databases visit the http://127.0.0.1:5984/_utils/index.html and select the remote_database. You can observe that the documents of local database (003, 004, 005) were copied in this database as shown below.

remote database

同样,如果你获取存储在 PouchDB 中 local_database 的内容,你可以观察到存储在 CouchDB 中的数据库的文档被复制到了这里。

In the same way, if you fetch the contents of the local_database stored in PouchDB you can get to observe that documents of the database that is stored in CouchDB were copied here.

[
   {
      id: '001',
      key: '001',
      value: { rev: '1-23cf3767e32a682c247053b16caecedb' },
      doc: {
         name: 'Geeta',
         age: 25,
         Designation: 'Programmer',
         _id: '001',
         _rev: '1-23cf3767e32a682c247053b16caecedb'
      }
   },
   {
      id: '002',
      key: '002',
      value: { rev: '1-d5bcfafbd4d4fae92fd7fc4fdcaa3a79' },
      doc: {
         name: 'Zara Ali',
         age: 24,
         Designation: 'Manager',
         _id: '002',
         _rev: '1-d5bcfafbd4d4fae92fd7fc4fdcaa3a79'
      }
   },
   {
      id: '003',
      key: '003',
      value: { rev: '1-bf4619471ac346fdde46cfa8fbf3587f' },
      doc: {
         name: 'Ram',
         age: 26,
         Designation: 'Programmer',
         _id: '003',
         _rev: '1-bf4619471ac346fdde46cfa8fbf3587f'
      }
   },
   {
      id: '004',
      key: '004',
      value: { rev: '1-29b8f803958c994e3eb37912a45d869c' },
      doc: {
         name: 'Robert',
         age: 27,
         Designation: 'Programmer',
         _id: '004',
         _rev: '1-29b8f803958c994e3eb37912a45d869c'
      }
   },
   {
      id: '005',
      key: '005',
      value: { rev: '1-0eb89f71998ffa8430a640fdb081abd2' },
      doc: {
         name: 'Rahim',
         age: 28,
         Designation: 'Programmer',
         _id: '005',
         _rev: '1-0eb89f71998ffa8430a640fdb081abd2'
      }
   }
]

你可以使用 sync() *method provided by PouchDB instead of the two methods *replicate.to()replicate.from() 重写上述程序,如下所示:

You can rewrite the above program using the sync() *method provided by PouchDB instead of the two methods *replicate.to() and replicate.from() as shown below.

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

//Creating local database object
var localDB = new PouchDB('local');

//Creating remote database object
var remoteDB = new PouchDB('http://localhost:5984/remote_database');

//Synchronising Remote and local databases
localDB.sync(remoteDB, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log(response);
   }
});

在执行上述程序时,它将同步这两个数据库并显示以下消息:

On executing the above program, it synchronizes the two databases displaying the following message.

{
   push: {
      ok: true,
      start_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time),
      docs_read: 6,
      docs_written: 6,
      doc_write_failures: 0,
      errors: [],
      last_seq: 10,
      status: 'complete',
      end_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time)
   },
   pull: {
      ok: true,
      start_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time),
      docs_read: 0,
      docs_written: 0,
      doc_write_failures: 0,
      errors: [],
      last_seq: 2,
      status: 'complete',
      end_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time)
   }
}

PouchDB - Miscellaneous

在本章中,我们将讨论诸如 compaction 和从 PouchDB 中检索批量数据之类的概念。

In this chapter, we will discuss the concepts like, compaction and retrieval of bulk data from PouchDB.

Compaction

你可以使用 compact() 方法删除未使用的数据库来减小其大小。你可以使用此方法压缩本地数据库和远程数据库。

You can reduce the size of a database by removing the unused data using compact() method. You can compact a local database as well as remote database using this method.

以下是展示 PouchDB 中 compact() 方法用法的示例。

Following is an example demonstrating the usage of the compact() method in PouchDB.

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

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

db.compact(function (err, result) {
   if (err) {
      return console.log(err);
   } else {
      console.log(result);
   }
});

BulkGet Method

你可以使用 bulkGet() 方法批量检索一组文档。你需要向此方法传递一组 id 和 _rev。

You can retrieve a set of documents in bulk using the bulkGet() method. To this method, you need to pass a set of id’s and _rev’s.

以下是展示 PouchDB 的 bulkGet() 方法用法的示例。

Following is an example demonstrating the usage of the bulkGet() method in PouchDB.

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

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

//Preparing documents
//Inserting Document
db.bulkGet({docs: [
   { id: "001", rev: "1-5dc593eda0e215c806677df1d12d5c47"},
   { id: "002", rev: "1-2bfad8a9e66d2679b99c0cab24bd9cc8"},
   { id: "003", rev: "1-7cff4a5da1f97b077a909ff67bd5b047"} ]}, function(err, result) {
   if (err) {
      return console.log(err);
   } else {
      console.log(result);
   }
});