📅  最后修改于: 2020-12-01 02:43:57             🧑  作者: Mango
db.get()方法用于读取或检索在数据库中创建的文档。此方法还接受文档ID和可选的回调函数。
句法:
db.get(document, callback)
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('Second_Database');
//Reading the contents of a Document
db.get('001', function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log(doc);
}
});
将以上代码保存在名为“ PouchDB_Examples”的文件夹中的名为“ Read_Document.js”的文件中。打开命令提示符,然后使用node执行JavaScript文件:
node Read_Document.js
它将读取存储在PouchDB服务器上“ Second_Database”中的文档。
{ name: 'Ajeet',
age: 28,
designation: 'Developer',
_id: '001',
_rev: '1-99a7a80ec2a74959885037a16d57924f' }
您可以从远程数据库(CouchDB)中读取或检索文档。为此,您必须在CouchDB中传递数据库的路径,该路径包含要读取的文档而不是数据库名称。
CouchDB服务器中有一个名为“员工”的数据库。
通过单击“员工”,您可以看到一个文档:
让我们获取该文档的数据:
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('http://localhost:5984/employees');
//Reading the contents of a document
db.get('001', function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log(doc);
}
});
将以上代码保存在名为“ PouchDB_Examples”的文件夹中的名为“ Read_Remote_Document.js”的文件中。打开命令提示符,然后使用node执行JavaScript文件:
node Read_Remote_Document.js
它将读取存储在CouchDB服务器上“员工”数据库中的文档。
输出:
{ _id: '001',
_rev: '3-276c137672ad71f53b681feda67e65b1',
name: 'Ajeet',
age: 28,
designation: 'Developer' }