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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:50:59+00:00 2026-05-26T06:50:59+00:00

Is it possible to pass custom javascript to Mongoid sort of like find_by_sql or

  • 0

Is it possible to pass custom javascript to Mongoid sort of like find_by_sql or update_all in active record?

The particular case I’m thinking of is I would like to inc a field on a bunch of documents matching a criteria.

It looks like this is possible in MongoDB:
http://www.mongodb.org/display/DOCS/Updating#Updating-ModifierOperations

db.people.update( { name:"Joe" }, { $inc: { created_at : -1 } } );

But I don’t thing Mongoid allows it (it seems to only let you inc one document at a time). And their update_all method I believe only can use $set. Thanks!
http://mongoid.org/docs/querying/modification.html

For example, this doesn’t work

User.where(:name => "Joe").update_all("{ $inc: { created_at : -1 } }")
  • 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-26T06:51:00+00:00Added an answer on May 26, 2026 at 6:51 am

    Mongoid offers a Model#inc method but it’s an instance method, not a class method.

    However, by reading the source code of the Model.update_all method it turns out you can easily create your own inc class method.

    def update_all(attributes = {})
      klass.collection.update(
        selector,
        { "$set" => attributes },
        Safety.merge_safety_options(:multi => true)
      ).tap do
        Threaded.clear_safety_options!
      end
    end
    

    As you can see, Mongoid is passing the query to the underlying mongodb driver.
    The following method should work

    class User
      def self.inc(field, value)
        klass.collection.update(
          selector,
          { "$inc" => { field => value } },
          Safety.merge_safety_options(:multi => true)
        ).tap do
          Threaded.clear_safety_options!
        end
      end
    end
    

    Please note I haven’t tested it yet.

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

Sidebar

Related Questions

I've implemented a MXML custom components and I would like to pass parameters to
Is it possible to sort Lucene results when using a custom collector, or would
Is it possible to pass a search variable into the Google Custom Search Engine
Possible Duplicate: Pass a PHP string to a Javascript variable (and escape newlines) I
Is it possible to pass a function/callback from javascript to a java applet? For
Is it possible to pass char* between 2 applications using a custom message in
I have created Custom .NET controls, and I'd like to know if it's possible,
I would like to pass a value to the ctor of a DbContext and
Is that possible? I want to pass my custom color as a parameter and
If I pass in a custom IComparer to an instance of a List's Sort()

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.