User documents count and size



I was wondering what are the methods for knowing how much documents a user has in a database how much size that very same documents occupy in a database.

The reason I’m looking for this is to somehow limit the number of documents a user can have in a certain plan tier.

Thanks in advance,

Best regards,



Number of documents is Database.count.
There’s no API to get the size of all the documents. You can look at the on-disk size of the db.sqlite3 file in the database’s directory, but that will include other stuff like indexes. Even if you could get the size of the documents, CBL stores them in a binary format with some compression, so it’s not the same as the size of the JSON documents on the server.


@jens thanks for your reply.

What other tactics you would suggest to meet my goal? I was thinking in count the documents I have on a local database after the sync happens. Can you tell me what you think of this idea and eventually suggest other ways?





Sure, you can count the docs whenever you want. I don’t really understand what you’re trying to do, though.


@jens thanks for your reply.

I want to limit the number of documents a user can have on a free account. Let’s say that a user can only have 100 documents using the free account. If he wants to have more than 100 documents he will need to pay an extra fee.

In order to do this I need to be able to prevent the user from adding more documents when the limit is reached. Also, knowing how much space a user documents are occupying is an important metric as well.

A user might have very little number of documents but if he has a lot of space being occupied by those documents, that would be a problem.

My application is a sound synthesizer . I have initially developed a preset synchronisation system on my own but it was not robust at all. I need a robust solution to scale the app capabilities and I have found couchbase.

Presets are just JSON files. But in the future, a preset can also have sound files (blobs). Since people get excited with this kind of ability, they start add documents over and over without having any concern about the implications of the database grow. I want to find ways of limiting this and getting extra revenue from the users who want to have more documents and space in order to be able to maintain and scale the complex system this involves.

Does this make sense to you? Can you see other problems arising?





Can’t you just put some code in the new-document handler that counts the docs and flags an error if the count is >= 100?

It doesn’t sound like document size will be an issue, but blobs definitely could be. Every blob has a length property with the size in bytes, so you could write a query to add all those up. In N1QL (supported by CBL_C now!) it would be like SELECT sum(soundFile.length).


Sorry my ignorance here but what is the “new-document handler”?


Oh, I just meant the app code that runs when the user presses whatever button is supposed to create a new document.


@jens alright! I thought it means something more particular or technical within couchbase context! :slight_smile: