How to ensure 2 queries that depend on one another

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.

Hi @Areeb_Ahmad,

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.

Regards,
Stennie

2 Likes

Thank you so much, I just really can’t tell how much it helped, I was at a dead end when encountered this problem

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