I am looking to add to multiple sets, and at the same time update the updated_at timestamp (touch). I could do this using the mongo driver:
db.mycollection.update({"_id": ObjectId("911")},
{
$addToSet: { "hashtags": {$each: ["#test1", "#test5"]}, "new_hash": {$each: ["test9"]} },
$set: {"updated": "current time 3"}
}
)
How can I do this using mongoid in a rails app, in a single update query. Right now, I need to do 3 writes using mongoid:
my_object.add_to_set("hashtags", ["#test1", "#test5"])
my_object.add_to_set("new_hash", ["test9"])
my_object.touch
You must use Moped, the Mongoid Driver (docs here: http://mongoid.org/en/moped/docs/driver.html).
something like this should do the trick:
my_query = {
'$addToSet' => { "hashtags" => {'$each' => ["#test1", "#test5"]}, "new_hash" => {'$each' => ["test9"]} },
'$set' => {"ts" => Time.now}
}
MyClass.collection.find('_id' => my_object.id).update(my_query)