[{ $match: { status: "completed" } }, { $out: "completed_sales" }]What is the result after running this pipeline?[{ $match: { status: "completed" } }, { $out: "completed_sales" }]The $out stage writes the aggregation results to a specified collection, replacing it if it exists. Here, it creates or replaces 'completed_sales' with only documents where status is 'completed'.
[{ $match: { category: "books" } }, { $merge: { into: "inventory", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }]What is the effect on the 'inventory' collection?
[{ $match: { category: "books" } }, { $merge: { into: "inventory", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }]The $merge stage merges documents into the target collection. With whenMatched: 'replace', matching documents are fully replaced. With whenNotMatched: 'insert', new documents are added.
{ $merge: { into: "users", on: "userId", whenMatched: "merge", whenNotMatched: "insert" } }The valid whenMatched values are 'replace', 'merge', 'keepExisting', and 'fail'. 'update' is not valid and causes a syntax error.
whenMatched: 'merge' updates only the fields in the incoming documents, which is efficient for large datasets where full replacement is costly.
$out and $merge regarding concurrent writes and atomicity?$out replaces the entire collection atomically, which blocks concurrent writes during the operation. $merge updates or inserts documents individually, allowing concurrent writes to other documents.