Hello Stack Overflow Community,
I am currently working on an Azure Cosmos DB project using the SDK and I have run into a situation where I need to limit the query response size to a maximum of 2MB. The necessity to cap the response size to 2MB arises from the fact that I am using the Container.CreateTransactionalBatch method afterwards, which has a limit of 2MB per batch.
Here is the current snippet of code where I am constructing the query:
var query = new QueryDefinition($"SELECT * FROM c WHERE c.Discriminator = '{_partitionKey}'");
var queryIterator = _sourceContainer.GetItemQueryIterator<T>(queryDefinition: query, continuationToken: _continuationToken);
While I know how to limit the number of items retrieved using QueryRequestOptions, I am unsure how to specifically limit the data size of the response to not exceed 2MB.
// An example of setting a max item count, which does not guarantee a 2MB size limit
var queryRequestOptions = new QueryRequestOptions { MaxItemCount = 100 };
I am looking for suggestions or approaches on how to achieve this.
Has anyone encountered a similar scenario or have insights on how this could be achieved efficiently without involving trial and error to find a suitable MaxItemCount?
Thank you!
Below are the steps I followed to limit the data size of the response:
Sets the
maxBatchSizeBytesvalue, which determines the maximum size for a batch of documents.Performs a SQL query to retrieve all documents from the container and saves them to documents.
Initializes variables to manage the batch processing of documents (
currentBatch,currentBatchSizeBytes, andbatchIndex).The
GetDocumentSizemethod is used to iteratively determine each document's size as it goes through the retrieved documents.It checks if adding the document to the current batch would exceed the
maxBatchSizeBytes. If not, it adds the document to the batch; otherwise, it processes the current batch and breaks out of the loop.GetDocumentSizemethod calculates the size of aMyDocumentobject in bytes.Below Is the whole data present In DB:
Below code Is used to limit the data size:
Output: