Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get document size in Cosmos DB

How can I get or figure out the size of a document stored in Cosmos DB?

Is this something already stored within document?

like image 205
Sam Avatar asked Oct 19 '17 04:10

Sam


2 Answers

Here is the way how to find the largest document in the collection. I used two queries in Azure's data explorer

  1. To get the size of the largest document SELECT MAX(a.length) FROM ( SELECT LENGTH(ToString(c)) AS length FROM c ) a

  2. To retrieve the body of the largest document (The a.length value should be compared to the length returned from the previous query SELECT TOP 1 a.c FROM ( SELECT LENGTH(ToString(c)) AS length, c FROM c ) a WHERE a.length = 382725

Then when running the second query you can peek the Query Stats tab to see the exact size of the document.

like image 189
Marcin Avatar answered Sep 17 '22 16:09

Marcin


If you get (or query) a document, you can then look at the headers that come back, specifically x-ms-resource-usage, which will contain a documentsSize attribute (representing a document's size in kb).

In node/javascript, you'd make a call that looks something like:

client.readDocument(docLink, function (err, doc, headers) {
  ...
})

You'd want to look at headers['x-ms-resource-usage'].

like image 45
David Makogon Avatar answered Sep 19 '22 16:09

David Makogon