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 7925271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:13:24+00:00 2026-06-03T18:13:24+00:00

Thought this would be pretty straight forward, but my value is remaining the same

  • 0

Thought this would be pretty straight forward, but my value is remaining the same (0).

What I’d like to do is increment my UnreadMessages field when the user receives a message they haven’t read and then decrement it when they have. So I thought code like this would work:

var userHelper = new MongoHelper<User>();
//increment
userHelper.Collection.Update(Query.EQ("Id", userId.ToId()), Update.Inc("UnreadMessages", 1));
//decrement
userHelper.Collection.Update(Query.EQ("Id", userId.ToId()), Update.Inc("UnreadMessages", -1));

After running these no errors are thrown but the value doesn’t change either. And no I’m not running one after the other as the code above could be interpreted 🙂

Update

Here’s my helper class:

public class MongoHelper<T> : Sandbox.Services.IMongoHelper<T> where T : class
{
    public MongoCollection<T> Collection { get; private set; }

    public MongoHelper()
    {
        var con = new MongoConnectionStringBuilder(ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString);
        var server = MongoServer.Create(con);
        var db = server.GetDatabase(con.DatabaseName);
        Collection = db.GetCollection<T>(typeof(T).Name.ToLower());
    }
}

and thanks to Travis’ answer I was able to pull this off:

MongoHelper<UserDocument> userHelper = new MongoHelper<UserDocument>();
            var user = userHelper.Collection.FindAndModify(Query.EQ("Username", "a"), SortBy.Null, Update.Inc("MessageCount", 1), true).GetModifiedDocumentAs<UserDocument>();
  • 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-06-03T18:13:26+00:00Added an answer on June 3, 2026 at 6:13 pm

    Not sure what your helper does. Here is a working snippet I use:

            var query = Query.And(Query.EQ("_id", keyName));
            var sortBy = SortBy.Null;
            var update = Update.Inc("KeyValue", adjustmentAmount);
            var result = collection.FindAndModify(query, sortBy, update, true);
    

    So, “query” finds the document, update does the increment, and FindAndModify puts them together and actually hits the database.

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

Sidebar

Related Questions

Okay at first I thought this would be pretty straightforward. But I can't think
I thought this would be pretty easy but I'm running into all sorts of
OK so I thought this would be pretty simple but it's tripping me up..
I thought this would be pretty straightforward, but I've been searching for a way
I thought this would be pretty simple, but it is proving a bit more
I thought this would be pretty simple to do but seems I must be
I thought this would be simple enough but somethings wrong. The only Bootstrap gem
I thought this would be simple, but its not working .. in both the
I thought this would be fairly easy, but I'm totally baffled. I want one
I would have thought this would be an easy one to Google, but I've

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.