Pouchdb 简明教程

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