Recover deleted documents?

Nope that’s it @Doug_Duncan !

Replace delete operation with update $set {deleted:true}.
And find operation should now include something like $exists deleted false to avoid including “soft” deleted documents unless you actually want to access these “deleted” docs. Then you can find and filter on {deleted:true}.

But @Doug_Duncan is also correct that this can lead to collections infinitely growing in size and an additional “deleted” field in all the indexes to support the queries (so more RAM).

Every now and then you will also want to actually delete the docs for real once they have been soft deleted for long enough.

For this, I would suggest using a TTL index on another additional field {deletedAt: new Date()} which would be set when the deleted field is set and it would actually delete automatically for real this time the docs after X seconds.

There is a trade-off for sure to consider.

Cheers,
Maxime.

2 Likes