We have a requirement to remove older documents based on an attribute when the count of the documents containing the said attribute goes over a threshold (which can be individually defined).
For eg:
collection_threshold: [
{
_id: abcd,
attributeId: id_for_threshold_1
threshold: 10
},
{
_id: abcde,
attributeId: id_for_threshold_2
threshold: 5
}
]
collection_events: [
{
uuid: id_for_threshold_1,
timestamp: 2022-04-01T11:00:00:00.000
...
},
{
uuid: id_for_threshold_1,
timestamp: 2022-03-31T11:00:00:00.000
...
},
{
uuid: id_for_threshold_2,
timestamp: 2022-04-01T11:00:00:00.000
...
},
{
uuid: id_for_threshold_2,
timestamp: 2022-03-31T11:00:00:00.000
...
}
]
In the above example the collection_events may hold only 10 documents with the uuid: id_for_threshold_1, when the 11th event gets written the oldest one (based on the timestamp) should automatically get deleted, similarly for the documents with the uuid: id_for_threshold_2, this should happen already for the 6th insert.
Is there a way to achieve this using mongo indexes?