I have a cosmosdb collection with each partition containing a set of documents.I would like to maintain the collection such that a logical partition('id' in this case) does not go over the limit of 5 documents. In the sample below, when a sixth entry(say for 8/11/2020) is added, I want to delete the document created on 7/13/2020 since that was updated the earliest. Basically, I want to make sure for item with id 12345, there are only 5 latest entries and no more. This is to reduce the data in the db and thus avoid querying more data than what's needed.
{
"id": 12345,
"lastUpdated": 8/10/2020
},
{
"id": 12345,
"lastUpdated": 8/3/2020
},
{
"id": 12345,
"lastUpdated": 7/27/2020
},
{
"id": 12345,
"lastUpdated": 7/20/2020
},
{
"id": 12345,
"lastUpdated": 7/13/2020
}
I could do something like this:
- Get all documents for id 12345
- If count of documents is >=5, get the last document (with instance 5) and delete it.
- Insert new document
However, that is a running 3 queries to insert a single document.
Is there a more elegant way to do this?
Thanks!