Is removing all documents from a collection before updating an anti-pattern?

I have a collection (B) that is created from 2 aggregation pipelines that are run off another collection (A). When something changes in collection A, collection B needs to be updated (deletions, updates, etc).

I’ve considered a number of strategies but it seems just wiping the collection and then running aggregation again seems the easiest. It just seems maybe 1) an anti-pattern 2) messes up my storage space - the indexes seemed to retain the same size even when the collection is empty.

If I drop the collection I need to recreate the indexes which is why I tried running a deleteMany().

Feedback appreciated !

May be you should consider using a view.

A view could work indeed as you are apparently just trying to present another “vision” of this collection.

Another solution could be to use Change Streams so you can reflect the relevant update & delete operations on the other collection. But a view would be much more simple if the aggregation that supports it isn’t too complicated.

Cheers,
Maxime.

If you create this collection using $out stage at the end of aggregation, then you don’t need to remove documents or drop the collection first, it will replace the existing collection with new one but preserving all the indexes that exist on it.

Asya

1 Like

True, but as I said it’s two separate pipelines so the second one overwrites the first. I considered using $out and just making two collections (1 for each pipeline) but right now that isn’t appealing to me.

1 Like

Thank you, I’m going to look into this later today. Also thank you to @steevej ! for the view recommendation as well.

1 Like