So, I have 2 queries and both of them are dependent on each other, let me explain:
When I send a campaign I want the campaign to be marked as send and get added to queue.
Schemas:
Queue:
{
status : String,
campaignId : mongooseId,
}
Campaign:
{
audience : [String],
status: String
}
Problem:
If I am saving the queue first and check for its successful execution then after that I updated the campaign status to “ongoing” to mark it as added to queue, if for some reason the campaign update query get failed I will be left with a document wasting memory.
If I go the other way I may end up marking the campaign as “ongoing” but without any queue.
I have encountered this problem so many times at so many different queries, please help me on how to overcome it.
If you have multi-document updates that depend on each other, you can use Transactions to ensure updates are consistent. I recommend reviewing the Production Considerations documentation for further information.
Note: transactions require a replica set or sharded cluster deployment. You can create a single member replica set for testing in development, but I would recommend a full replica set deployment (three members) for a production deployment. You can also test transactions with a MongoDB Atlas Free Tier Cluster.