-
Notifications
You must be signed in to change notification settings - Fork 29
DOCSP-45364: CRUD pt 1 #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rustagir
merged 12 commits into
mongodb:standardized
from
mayaraman19:DOCSP-45364-crud1
Jan 10, 2025
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7dc4d73
checkpoint
mayaraman19 544870f
checkpoint 2
mayaraman19 fc3aa39
woohoo first pass
mayaraman19 33ed0ad
indent
mayaraman19 fffc2c8
Edits
mayaraman19 17da987
updates
mayaraman19 4731550
Merge branch 'standardized' of https://github.com/mongodb/docs-mongoi…
mayaraman19 8f859eb
vale chekcs
mayaraman19 4f1810a
RR PR fixes 1
rustagir 0d113ae
fix code file
rustagir 5816cf8
code fixes
rustagir f068dc1
RM PR fixes 1
rustagir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
# start create! example | ||
Person.create!( | ||
first_name: "Heinrich", | ||
last_name: "Heine" | ||
) | ||
|
||
Person.create!([ | ||
{ first_name: "Heinrich", last_name: "Heine" }, | ||
{ first_name: "Willy", last_name: "Brandt" } | ||
]) | ||
|
||
Person.create!(first_name: "Heinrich") do |doc| | ||
doc.last_name = "Heine" | ||
end | ||
# end create! example | ||
|
||
# start create example | ||
Person.create( | ||
first_name: "Heinrich", | ||
last_name: "Heine" | ||
) | ||
|
||
class Post | ||
include Mongoid::Document | ||
validates_uniqueness_of :title | ||
end | ||
|
||
posts = Post.create([{title: "test"}, {title: "test"}]) | ||
posts.map { |post| post.persisted? } # => [true, false] | ||
# end create example | ||
|
||
# start save! example | ||
person = Person.new( | ||
first_name: "Esmeralda", | ||
last_name: "Qemal" | ||
) | ||
person.save! | ||
|
||
person.first_name = "Malik" | ||
person.save! | ||
# end save! example | ||
|
||
# start save example | ||
person = Person.new( | ||
first_name: "Tamara", | ||
last_name: "Graham" | ||
) | ||
person.save | ||
|
||
person.first_name = "Aubrey" | ||
person.save(validate: false) | ||
# end save example | ||
|
||
# start attributes example | ||
person = Person.new(first_name: "James", last_name: "Nan") | ||
person.save | ||
|
||
puts person.attributes | ||
# end attributes example | ||
|
||
# start reload example | ||
band = Band.create!(name: 'Sun 1') | ||
# => #<Band _id: ..., name: "Sun 1"> | ||
|
||
band.name = 'Moon 2' | ||
# => #<Band _id: ..., name: "Moon 2"> | ||
|
||
band.reload | ||
# => #<Band _id: ..., name: "Sun 1"> | ||
# end reload example | ||
|
||
# start reload unsaved example | ||
existing = Band.create!(name: 'Photek') | ||
|
||
band = Band.new(id: existing.id) | ||
band.reload | ||
|
||
puts band.name | ||
# end reload unsaved example | ||
|
||
# start update attributes! example | ||
person.update_attributes!( | ||
first_name: "Maximilian", | ||
last_name: "Hjalmar" | ||
) | ||
# end update attributes! example | ||
|
||
# start update attributes example | ||
person.update_attributes( | ||
first_name: "Hasan", | ||
last_name: "Emine" | ||
) | ||
# end update attributes example | ||
|
||
# start update attribute example | ||
person.update_attribute(:first_name, "Jean") | ||
# end update attribute example | ||
|
||
# start upsert example | ||
person = Person.new( | ||
first_name: "Balu", | ||
last_name: "Rama" | ||
) | ||
person.upsert | ||
|
||
person.first_name = "Ananda" | ||
person.upsert(replace: true) | ||
# end upsert example | ||
|
||
# start touch example | ||
person.touch(:audited_at) | ||
# end touch example | ||
|
||
# start delete example | ||
person.delete | ||
person = Person.create!(name: 'Edna Park') | ||
|
||
unsaved_person = Person.new(id: person.id) | ||
unsaved_person.delete | ||
person.reload | ||
# end delete example | ||
|
||
# start destroy example | ||
person.destroy | ||
# end destroy example | ||
|
||
# start delete all example | ||
Person.delete_all | ||
# end delete all example | ||
|
||
# start destroy all example | ||
Person.destroy_all | ||
# end destroy all example | ||
|
||
# start new record example | ||
person = Person.new( | ||
first_name: "Tunde", | ||
last_name: "Adebayo" | ||
) | ||
puts person.new_record? | ||
|
||
person.save! | ||
puts person.new_record? | ||
# end new record example | ||
|
||
# start persisted example | ||
person = Person.new( | ||
first_name: "Kiana", | ||
last_name: "Kahananui" | ||
) | ||
puts person.persisted? | ||
|
||
person.save! | ||
puts person.persisted? | ||
# end persisted example | ||
|
||
# start field values default | ||
class Person | ||
include Mongoid::Document | ||
field :first_name | ||
end | ||
|
||
person = Person.new | ||
|
||
person.first_name = "Artem" | ||
person.first_name # => "Artem" | ||
# end field values default | ||
|
||
# start field values hash | ||
class Person | ||
include Mongoid::Document | ||
|
||
field :first_name, as: :fn | ||
end | ||
|
||
person = Person.new(first_name: "Artem") | ||
|
||
person["fn"] | ||
# => "Artem" | ||
|
||
person[:first_name] = "Vanya" | ||
# => "Artem" | ||
|
||
person | ||
# => #<Person _id: ..., first_name(fn): "Vanya"> | ||
# end field values hash | ||
|
||
# start read write attributes | ||
class Person | ||
include Mongoid::Document | ||
|
||
def first_name | ||
read_attribute(:fn) | ||
end | ||
|
||
def first_name=(value) | ||
write_attribute(:fn, value) | ||
end | ||
end | ||
|
||
person = Person.new | ||
|
||
person.first_name = "Artem" | ||
person.first_name | ||
# => "Artem" | ||
# end read write attributes | ||
|
||
# start read write instance | ||
class Person | ||
include Mongoid::Document | ||
field :first_name, as: :fn | ||
end | ||
|
||
person = Person.new(first_name: "Artem") | ||
# => #<Person _id: ..., first_name(fn): "Artem"> | ||
|
||
person.read_attribute(:first_name) | ||
# => "Artem" | ||
|
||
person.read_attribute(:fn) | ||
# => "Artem" | ||
|
||
person.write_attribute(:first_name, "Pushkin") | ||
|
||
person | ||
# => #<Person _id: ..., first_name(fn): "Pushkin"> | ||
# end read write instance | ||
|
||
# start attributes= example | ||
person.attributes = { first_name: "Jean-Baptiste", middle_name: "Emmanuel" } | ||
# end attributes= example | ||
|
||
# start write_attributes example | ||
person.write_attributes( | ||
first_name: "Jean-Baptiste", | ||
middle_name: "Emmanuel", | ||
) | ||
# end write_attributes example | ||
|
||
# start atomically example | ||
person.atomically do | ||
person.inc(age: 1) | ||
person.set(name: 'Jake') | ||
end | ||
# end atomically example | ||
|
||
# start default block atomic example | ||
person.atomically do | ||
person.atomically do | ||
person.inc(age: 1) | ||
person.set(name: 'Jake') | ||
end | ||
raise 'An exception' | ||
# Name and age changes are persisted | ||
end | ||
# end default block atomic example | ||
|
||
# start join_contexts atomic | ||
person.atomically do | ||
person.atomically(join_context: true) do | ||
person.inc(age: 1) | ||
person.set(name: 'Jake') | ||
end | ||
raise 'An exception' | ||
# Name and age changes are not persisted | ||
end | ||
# end join_contexts atomic |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the purpose of this line. I think the example is more clear without it unless I'm missing something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it was left over from testing?