2

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?

iFunction
  • 1,208
  • 5
  • 21
  • 35

2 Answers2

5

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

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
5

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.

seb
  • 808
  • 1
  • 9
  • 17