Couchdb, return number of Docs

6k Views Asked by At

How do I do this simple thing, I just want the number of documents in a database. So far I am using a loop function in python to do this, but it seems an incredible inefficient way of doing this. Fauton tells you at the bottom of the page how many docs there are, how can i get this number in either python or javascript please?

2

There are 2 best solutions below

2
OrangeDog On BEST ANSWER

The relevant API call is GET /{db}. In the python driver, it is db.info().

0
seb On

When you send a GET request to the database root, as per OrangeDog's answer, i.e.: GET http://couchdb.server.com/mydatabase you will get a JSON back, looking similar to the following:

{
 "db_name":"mydatabase",
 "update_seq":"2786-g1AAAAFreJzLYWBg4MhgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUoxJTIkyf___z8riYGB0RuPuiQFIJlkD1Naik-pA0hpPExpDj6lCSCl9TClwXiU5rEASYYGIAVUPR-sPJqg8gUQ5fvBygMIKj8AUX4frDyOoPIHEOUQt0dlAQB32XIg",
 "sizes":{
    "file":13407816,
    "external":3760750,
    "active":4059261
 },
 "purge_seq":0,
 "other": {
    "data_size":3760750
 },
 "doc_del_count":0,
 "doc_count":2786,
 "disk_size":13407816,
 "disk_format_version":6,
 "data_size":4059261,
 "compact_running":false,
 "instance_start_time":"0"
}

The field doc_count is a count of all documents in the database. Bear in mind that the count includes design documents, that might not be what you are looking for.

There are other options, like creating a view with a default reduce function _count that would allow you to get a single number representing a number of documents generated in that view, that could get around the issue of counting in design documents.