standard persistence
Mongoid's standard persistence methods come in the form of common methods you would find in other mapping frameworks.
- Model.create
- Model.create!
- Model#save
- Model#save!
- Model#update_attributes
- Model#update_attributes!
- Model#update_attribute
- Model#delete
- Model#destroy
- Model.delete_all
- Model.destroy_all
Model.create
Inserts a new document into the database given the provided attributes. This will run validations and will return the document whether it was persisted or not. You can check Model#persisted? to see if it was successful.
# Insert a new German poet to the db. Person.create(first_name: "Heinrich", last_name: "Heine") # This can also take a block. Person.create(first_name: "Heinrich") do |doc| doc.last_name = "Heine" end
collections["people"].insert({ "_id" : ..., "first_name" : "Heinrich", "last_name" : "Heine" })
Model.create!
Inserts a new document into the database given the provided attributes. This will run validations and will return the document if it passed validations, otherwise it will raise a validations error.
# Insert a new German poet to the db, raising an error if # validation failed. Person.create!(first_name: "Heinrich", last_name: "Heine") # This can also take a block. Person.create!(first_name: "Heinrich") do |doc| doc.last_name = "Heine" end
collections["people"].insert({ "_id" : ..., "first_name" : "Heinrich", "last_name" : "Heine" })
Model#save
Saves the document to the database. If the document is new then the entire document will be inserted. If the document is already saved then only changes to the document will the persisted. This runs validations by default, however they can be switched off by providing an option to the method. Returns true if validation passed and false if not.
# Insert a new German poet to the db. person = Person.new(first_name: "Heinrich", last_name: "Heine") person.save # Save without running validations. person.save(validate: false) # Save an existing document's changed fields. person.first_name = "Christian Johan" person.save
# Insert command for the new document. collections["people"].insert({ "_id" : ..., "first_name" : "Heinrich", "last_name" : "Heine" }) # Update command for the changed document. collections["people"].update({ { "_id" : ... }, { "$set" : { "first_name" : "Christian Johan" } } })
Model#save!
Saves the document to the database. If the document is new then the entire document will be inserted. If the document is already saved then only changes to the document will the persisted. Returns true if validation passed and raises an error if not.
# Insert a new German poet to the db. person = Person.new(first_name: "Heinrich", last_name: "Heine") person.save! # Save an existing document's changed fields. person.first_name = "Christian Johan" person.save!
# Insert command for the new document. collections["people"].insert({ "_id" : ..., "first_name" : "Heinrich", "last_name" : "Heine" }) # Update command for the changed document. collections["people"].update({ { "_id" : ... }, { "$set" : { "first_name" : "Christian Johan" } } })
Model#update_attributes
Modifies the provided attributes to new values and persists them in a single call. This runs validations and will return true if they passed, false if not.
# Update the provided attributes. person.update_attributes(first_name: "Jean", last_name: "Zorg")
# Update command for the changed document. collections["people"].update({ { "_id" : ... }, { "$set" : { "first_name" : "Jean", "last_name" : "Zorg" } } })
Model#update_attributes!
Modifies the provided attributes to new values and persists them in a single call. This runs validations and will return true if they passed or raise an error if not.
# Update the provided attributes. person.update_attributes!(first_name: "Jean", last_name: "Zorg")
# Update command for the changed document. collections["people"].update({ { "_id" : ... }, { "$set" : { "first_name" : "Jean", "last_name" : "Zorg" } } })
Model#update_attribute
Updates a single attribute in the database without going through the normal validation procedure, but does fire callbacks. Returns true if save was successful, false if not.
# Update the provided attribute. person.update_attribute(:first_name, "Jean")
# Update command for the changed document. collections["people"].update({ { "_id" : ... }, { "$set" : { "first_name" : "Jean" } } })
Model#delete
Deletes the document from the database without running callbacks.
person.delete
collections["people"].remove("_id" : ... )
Model#destroy
Deletes the document from the database while running destroy callbacks.
person.destroy
collections["people"].remove("_id" : ... )
Model.delete_all
Deletes all matching documents in the database given the supplied conditions. See the criteria section on deletion for preferred ways to perform these actions. This does not run any callbacks on the matching documents.
# Delete all the documents from the collection. Person.delete_all # Delete all matching documents. Person.delete_all(conditions: { first_name: "Heinrich" })
# Delete all command. collections["people"].remove # Delete all matching command. collections["people"].remove("first_name" : "Heinrich")
Model.destroy_all
Deletes all matching documents in the database given the supplied conditions. See the criteria section on deletion for preferred ways to perform these actions. This runs destroy callbacks on all matching documents.
# Destroy all the documents from the collection. Person.destroy_all # Destroy all matching documents. Person.destroy_all(conditions: { first_name: "Heinrich" })
# Destroy all command. collections["people"].remove # Destroy all matching command. collections["people"].remove("first_name" : "Heinrich")