Dynamically set partition key for Cosmos

A common pattern is set Cosmos partition keys on a per-query basis. For example, if the partition key is set to year-month (yyyy-MM) to distribute the data over time intervals the query will have to update the partition key based on the year-month of interest.

Meantime, is there a workaround which allow the partition key to be set at runtime?

1 Like

Could you use ${new Date().getFullYear()}-${(new Date().getMonth() + 1).toString().padStart(2, '0')} as the value for the partition key field in your query to insert the 'YYYY-MM' value?

Thank you for your quick response. I'm new to ReTool and assumed that I couldn't update the query.partitionKeys string state variable. I'll try to do that via JavaScript before submitting the query.

Sounds good; let us know how it goes.

And welcome to the forums!