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 } }")
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_allmethod it turns out you can easily create your ownincclass method.As you can see, Mongoid is passing the query to the underlying mongodb driver.
The following method should work
Please note I haven’t tested it yet.