penguinnnnn
penguinnnnn

Reputation: 61

Show total number of rows in pouchdb

I have some data in the pouchdb, I want to show the total rows in pouchdb, how can I do it?

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);
    }
}]);

Upvotes: 3

Views: 3680

Answers (2)

Phonolog
Phonolog

Reputation: 6511

Using promises and the allDocs function, getting the number of documents could look like this in Typescript. I'm assuming the $pouchDB variable holds your PouchDB.

$pouchDB.allDocs().then(entries => console.log(entries.rows.length));

A plain javascript solution with callbacks could look like this:

$pouchDB.allDocs(function(err, response) {
    if (err) { return console.log(err); }
    console.log(response.rows.length);
});

Note: See the docs for more information about the allDocs function.

And make sure to call the allDocs function with the include_docs parameter if you actually want to get the documents. By default, you will only get the _id and _rev properties, not the whole document.

Actually fetching all documents could look like this:

$pouchDB.allDocs({
  include_docs: true
}).then(entries => console.log(entries));

Upvotes: 3

cjroth
cjroth

Reputation: 597

@Phonolog suggested allDocs() but I would recommend using info() as this has less overhead and will be faster.

$pouchDB.info().then(info => console.log(info.doc_count))

Upvotes: 11

Related Questions