Home > Enterprise >  CouchDB count all documents and return a number?
CouchDB count all documents and return a number?

Time:10-04

We have a college project in CouchDB and I'm using node, I want to create a view that returns a number of all my documents by email.

I cannot find anything that works and I'm not sure what I'm missing, I tried a lot of different reduce functions and emit methods.

Thanks for any answers.

The documents have 2 fields, name and email

enter image description here

enter image description here

CodePudding user response:

You can use GET /{db}, which returns information about the specified database. This is a JSON object that contains the property doc_count.

doc_count (number) – A count of the documents in the specified database.

With Angular for example, this could be done with the following method:

async countDocuments(database: string): Promise<number> {
  return this.http.get<any>(this.url('GET', database), this.httpOptions).toPromise()
    .then(info => info['doc_count']);
}
  • Related