Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid multiple atomic operations

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?

like image 573
Finbarr Avatar asked Nov 23 '22 09:11

Finbarr


1 Answers

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"}})
like image 89
amit_saxena Avatar answered Dec 21 '22 16:12

amit_saxena