显示 pouchdb 中的总行数
Show total number of rows in pouchdb
我在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);
});
注意:有关 allDocs
函数的更多信息,请参阅 docs。
如果您真的想获取文档,请务必使用 include_docs
参数调用 allDocs
函数。默认情况下,您只会获得 _id
和 _rev
属性,而不是整个文档。
实际获取所有文档可能如下所示:
$pouchDB.allDocs({
include_docs: true
}).then(entries => console.log(entries));
@Phonolog 建议 allDocs()
,但我建议使用 info()
,因为这样开销更小而且速度更快。
$pouchDB.info().then(info => console.log(info.doc_count))
我在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);
});
注意:有关 allDocs
函数的更多信息,请参阅 docs。
如果您真的想获取文档,请务必使用 include_docs
参数调用 allDocs
函数。默认情况下,您只会获得 _id
和 _rev
属性,而不是整个文档。
实际获取所有文档可能如下所示:
$pouchDB.allDocs({
include_docs: true
}).then(entries => console.log(entries));
@Phonolog 建议 allDocs()
,但我建议使用 info()
,因为这样开销更小而且速度更快。
$pouchDB.info().then(info => console.log(info.doc_count))