safe mode

For cases when you want to persist with or without safe mode on a per-query basis you can use Mongoid's provided #safely and #unsafely methods. These can be prepended to any persistence operation Mongoid provides.

persisting in safe mode

When you are using the default configuration settings and Mongoid's persist_in_safe_mode option is false, you may persist a single operation in safe mode by overriding it with #safely.

Using safely, you can have the following options:

  • fsync: Whether to perform an fsync.
  • w: The number of nodes to write to before returning.
  • wtimeout: The timeout value for writing to multiple nodes.
Person.safely.create(name: "Bond")
Person.safely(w: 3).create!(name: "James")
person.safely.save
person.safely(fsync: true).save!
person.safely(w: 2, wtimeout: 5000).update_attributes(title: "Sir")
person.safely.update_attributes!(title: "Madam")

persisting without safe mode

When you override the default configuration settings and Mongoid's persist_in_safe_mode option is true, you may persist a single operation without safe mode by overriding it with #unsafely.

Person.unsafely.create(name: "Bond")
Person.unsafely.create!(name: "James")
person.unsafely.save
person.unsafely.save!
person.unsafely.update_attributes(title: "Sir")
person.unsafely.update_attributes!(title: "Madam")