Reputation: 1630
I'm using Rails 4.1.4 and Mongoid in my app.
I just wanted to know if there's a way to perform a block transaction atomically using Mongoid in Rails.
To make things clear, suppose I have this:
some_model_instance = SomeModel.find(some_id)
some_model_instance.some_attribute = "Some Attribute Value"
some_model_instance.save
other_model_instance = OtherModel.find(other_id)
other_model_instance.other_attribute = "Other Attribute Value"
other_model_instance.save
I want to wrap that code in a single atomic transaction, I mean, if something goes wrong I want neither of both instances (some_model_instance, other_model_instance) to be saved to the database.
I've seen that with ActiveRecord is somehow like this:
SomeModel.transaction do
# do stuff I did above
end
Does that work also with Mongoid?.
Thanks for any help!!!.
Upvotes: 2
Views: 1817
Reputation: 9747
It's an old question but just adding an update for future visitors. MongoDB has introduced multi-document transactions which help to bundle multiple document transactions as one.
From Mongo's documentation:
For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.
Here is the reference for details: https://www.mongodb.com/docs/manual/core/transactions
Upvotes: 0
Reputation: 1630
CLOSED QUESTION
MongoDB does not support transactions. It only performs atomic operations for a single document. So there's no way to make an "Atomic Block Transaction" with Mongoid.
Best.
Upvotes: 7