2

I'm using Mongoid, but all I need to do is increment a field which is an integer. Would it be worth it for me to skip using Mongoid's methods, and just run the mongodb ruby drivers method to increment a value?

How would I do that?

2 Answers 2

2

Mu is too short is correct, you could either use the built in mongoid #inc method:

Model.inc(:field, integer)

Or you could access the collection directly via the collection attribute:

Model.collection.update(
  { query document },
  { "$inc" => { :field => value } }
)
Sign up to request clarification or add additional context in comments.

Comments

2

Why not use Mongoid's inc method?

Model#inc

Performs MongoDB's $inc modifier which increments it's value by the supplied amount or initializes it to that value. If the field is not numeric an error will be raised.

So doing model.inc(:a, 11) should send an { $inc: { a: 11 } } update straight into MongoDB.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.