arpanchaudhury
arpanchaudhury

Reputation: 237

Ensuring data persistence in MongoDB

I want to be sure whether my data gets persisted successfully in MongoDB. As in some cases MongoDB takes a fire_and_forget strategy, I want to specify Write Concern {w : majority, j : 1} at driver level which in my case is Mongoid.

Use-case :

I want to ensure my Users have unique 'nickname' and cannot signup violating the uniqueness.

I have already created an Unique Index on 'nickname' field.

Upvotes: 0

Views: 4199

Answers (2)

Derick
Derick

Reputation: 36764

For replica sets you can use the following configuration, as is described at http://mongoid.org/en/mongoid/docs/installation.html#replica:

consistency: :strong

Together with that, you'd want to have safe mode on, as is described at http://mongoid.org/en/mongoid/docs/tips.html#safe_mode:

safe: true

It does not look like you can set MongoDB's w parameter like this, but you can set it on a Band document operation—that's going to be per query though:

Band.with(safe: { w: 3 })

You can also do it per session with:

Band.mongo_session.with(safe: { w: 3 }) do |session|
    session[:artists].find(...)
end

Upvotes: 2

Woot4Moo
Woot4Moo

Reputation: 24316

Short answer: you can't.

Long answer:
Consider using multiple data storage options. Far too often people are jumping on the NoSQL bandwagon, when it isn't necessary. If you need guranteed writes you should use a relational database, or consider a hybrid such as orientDB. Lack of guaranteed writes is one of the big reasons why solutions such as MongoDB scale so well.

Upvotes: 0

Related Questions