I have a kind of Mongoid::Document
in memory. I want to atomically inc
and push
in the same call. Mongoid::Criteria
only exposes these operations individually.
my_model = SomeModel.find "foo"
Bad:
my_model.inc foo: 1
my_model.push bar: "b"
Good:
my_model.underlying_adapter.update "$inc" => {foo: 1}, "$push" => {bar: "b"}
The question is, how do I access that underlying adapter for a single instance of a Mongoid::Document
?
You can use moped (the ruby adapter which mongoid uses) directly for this and other complex atomic operations which you want to achieve in a single query:
SomeModel.collection.find("_id" => "foo").update({
'$inc' => {"foo" => 1},
'$push' => {"bar" => "b"}})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With