Get total number of documents in a channel in sync gateway [more of curioisty please]

Google indicated it is not possible but there is no way to get the total number of documents in a specific channel at the time of call? I was asked the question today in a product demo and didn’t have an answer.

Why is it not supported?

There’s usually going to be additional data (i.e. document IDs) required to make this type of query useful for somebody, unless you’re just looking for empty and non-empty channels, so there’s no API that returns only channel document count.

However, you can use the changes REST API which returns a small amount of metadata for each doc, and apply a channel filter using the following query params:

:4985/db/_changes?filter=sync_gateway/bychannel&channels=foo&active_only=true
{"results":[

{"seq":16,"id":"doc1","changes":[{"rev":"1-b451d5cf23dccace31249cfe4d631738"}]}
,{"seq":17,"id":"doc2","changes":[{"rev":"1-818eed1d045807e6d4045884545137d2"}]}
],
"last_seq":"17"}

And piping this into jq is quite easy to get a simple item count

$ curl -s "http://...:4985/db/_changes?filter=sync_gateway/bychannel&channels=foo&active_only=true" | jq '.results | length'

2