MongoDB atlas archive after total document reaches a certain number

I want to write a custom archive rule in mongodb that archives the data with some condition.

Let’s say I have a collection A.

If the collection has more than 1000 docs, archive the oldest doc (I have createdAt) until total document count is 1000. So basically, It should not exceed 1000.

Alternatively, I would also love if I was able to archive oldest documents if the total size of the collection reaches a certain threshold. How would I do it?

Hey Roshan,

This is really interesting, it’s the first time we’ve gotten the request of archiving based a collection state condition like count and specifically maintaining a certain number of documents in a collection.

I don’t believe there is anyway to do either of these things within the rule itself. But you can maintain this state outside of the rule, and trigger archiving with something like a check for a field in a document like “archive” : true .

Then you could monitor your collection using a Realm Trigger to check if there are more than X documents in the collection and set the appropriate document to “archive”: true .

We’ll take these and put them in the backlog though, if you want you can create a feature request for them here: Thanks for the requests, keep them coming!

1 Like