我在pouchdb中有一些数据,我想在pouchdb中显示总行数,我该怎么办?
angular.module("pouchapp", ["ui.router"])
.run(function($pouchDB) {
$pouchDB.setDatabase("dbinfo");
//console.log($pouchDB.length); //show total rows in pouchdb
});
.service("$pouchDB", ["$rootScope", "$q", function($rootScope, $q) {
var database;
this.setDatabase = function(databaseName) {
database = new PouchDB(databaseName);
}
}]);
使用promises和allDocs
函数,获取文档的数量在Typescript中可能如下所示。我假设$pouchDB
变量拥有你的PouchDB。
$pouchDB.allDocs().then(entries => console.log(entries.rows.length));
带回调的普通javascript解决方案可能如下所示:
$pouchDB.allDocs(function(err, response) {
if (err) { return console.log(err); }
console.log(response.rows.length);
});
注意:有关docs函数的更多信息,请参阅allDocs
。
如果你真的想要获取文件,请务必使用allDocs
参数调用include_docs
函数。默认情况下,您只能获取_id
和_rev
属性,而不是整个文档。
实际上获取所有文档可能如下所示:
$pouchDB.allDocs({
include_docs: true
}).then(entries => console.log(entries));
@Phonolog建议使用allDocs()
,但我建议使用info()
,因为这样可以减少开销并提高速度。
$pouchDB.info().then(info => console.log(info.doc_count))