Pouchdb 简明教程
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.
以下是如何阅读存储在 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'
}
}
]