Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5948447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:06:31+00:00 2026-05-22T17:06:31+00:00

I have a collection of messages: { messageid: ObjectId userid: ObjectId message: string isread:

  • 0

I have a collection of messages:

{
    messageid: ObjectId
    userid: ObjectId
    message: string
    isread: true|false
}

and a collection of message counts per user:

{
    userid: ObjectId
    total: int
    unread: int
}

When I delete a message from “messages” collection, I also need to decrease “total” in “counts” collection, and conditionally (if “messages.isread” == false) decrease “unread” field as well.

For that I need to first retrieve teh message, check its “isread” field, and then update the counts. There is a possibility that the message will be marked as read in between those actions, then I will decrease “unread” counts incorrectly.

Is there a way to conditionally change something in one collection based on results of other collection in one shot?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:06:32+00:00Added an answer on May 22, 2026 at 5:06 pm

    There are lots of answers here, but I want to fill in all of the blanks here:

    Is there any way to atomically update two collections in MongoDB?

    No. Atomic update of two collections is effectively a transaction. MongoDB does not support transactions across collections or even within a collection.

    MongoDB provides several modifiers that are atomic on a single document. So you can increment several different variables at once ($inc). Though there are some limitations here, you cannot perform two different operations on a single property.

    Is there a way to conditionally change something in one collection based on results of other collection in one shot?

    There are some documents here on atomic updates in general. However, what you really need is a queue and some form of two-phase commit or you need triggers.

    Triggers have not yet been implemented, so it’s not really an option in your case.

    There is a possibility that the message will be marked as read in between those actions, then I will decrease “unread” counts incorrectly.

    At this point, you have a couple of different strategies for making this behave with some level of consistency. Frankly, based on your description, you may want to investigate building a simple queue that updates your totals.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a collection of Messages which has the properties UserID (int)
I have documents collection Messages in my base (RavenDB) Document definition like: class Message
I have collection of documents Message in RavenDB. Definition: class Message { string Content;
I have got an Entity model which contains a collection of Message objects which
I have a set of messages, that all derive from Message, and implement the
I have a relatively simple Backbone handled message container. I can add messages, and
I have some XML: <messages> <message> <meta appid=1112 date=20111028 msgid=4498016 msgno=855 permlinkdate=2011-10-28> <summary>RedBook is
I have following collection view <CollectionViewSource x:Key=messages Source={Binding src}> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName=Group/> </CollectionViewSource.GroupDescriptions> </CollectionViewSource>
In protocol buffers is there a way to have a message with collection of
during beta testing we discovered connection pooling error messages . Therefore I have been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.