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

  • Home
  • SEARCH
  • 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 3619782
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:55:20+00:00 2026-05-18T22:55:20+00:00

I want to update a field in a database table that has to have

  • 0

I want to update a field in a database table that has to have a cumulative value. So basically I need to find the current value of the field and update it using a new number.

My first inefficient try at this (in Mongoid) is:

v = Landlord.where(:name=>"Lorem")
v.update_attributes(:violations=>v.violations + 10)

Is there a simple method than making one query to read, then sum up, and another query to write?

  • 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-18T22:55:20+00:00Added an answer on May 18, 2026 at 10:55 pm

    MongoDB provides special atomic update operation support for exactly this scenario — take a look at the $inc modifier in the Updating documentation. Your example in the plain mongo console would be:

    db.landlords.update( { name:"Lorem" }, { $inc: { violations : 10 } } );
    

    With Mongoid this would be:

    Landlord.where(:name => "Lorem").first.inc(:violations, 10)
    

    This will set violations to 10 if the field is defined on your model but hasn’t otherwise been initialized to any value yet.

    But actually, this still executes two queries (look up the landlord, then update it). To do it all in one, you need to go through a Mongoid::Collection object, which proxies most calls straight through to the Mongo Ruby driver. That would look like this:

    Landlord.collection.update({ :name => "Lorem" }, '$inc' => { :violations => 10 })
    

    It helps to be familiar with the Ruby driver for cases like this when you want to take more direct control. Its API tries to match closely with the mongo Javascript shell syntax.

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

Sidebar

Related Questions

I have a field in my mysql table that I want to update. Example:
I want to uapdate a table field with an array.That means i have a
If I have a LINQ to SQL table that has a field called say
I have a table in database that has 2 columns Name | Age, I
Some background: I have an database that I want to use linq-to-sql to update
i want to update my sqlite database but i cannot find the way to
I have a MySQL table from a third-party application that has millions of rows
I have an entity model that has audit information on every table (50+ tables)
in my query i want to update row of table tblUser_Report. it has date
Here is a simple example: Assume I have a customer table that has a

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.