Automatic document deletion

Hello guys

Lets say you have a document and this document has an array, you want this document to automatically get deleted on the collection as soon as the array runs/gets empty

How do u do that? Because i have some documents with empty arrays still hanging around but i want them deleted automatically when that happens

Pleasd help

Hi @Tumelo_waheng welcome to the community!

I don’t believe we have such a feature at the moment. You can expire documents based on time using a TTL index but not based on arbitrary conditions. Currently you might need a scheduled process using db.collection.deleteMany() or implement the removal in the application side.

In the meantime, if this is an important feature for you, please provide a feedback in the MongoDB Feedback Engine.

Best regards
Kevin

In addition to what Kevin has stated above, you could possibly achieve the delete by using Database Triggers (if your deployment is on Atlas) or Change Streams.

Regards,
Jason

2 Likes

Auto-delete the whole Databases after a certain amount of time in mongodb compass please help in.

Thank you