Materialized View delete

Hi,
I’d like to flush all data in Materialized view every-time when I update.
Should I just delete the collection and use merge to create it again??

Hi @Peter_li1,

It depends. You can leverage the different options of the $merge stage like

  • whenMatched: <replace|keepExisting|merge|fail|pipeline>
  • whenNotMatched: <insert|discard|fail>

See: https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge/#syntax

If you have an exact match on a field each time then you could just use “replace” and if you have a 1 to 1 match for every single document, the entire collection would be replaced in the end.

Or you could use $out instead of $merge which would replace the target the collection.

I think this last option is probably the best because it will preserve collection metadata (like indexes).

Cheers,
Maxime.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.