mongodb - Mongoid embed_one and has_one -
recently working on mongoid, i'm confused embed_one in mongoid same has_one? if no, what's difference , examples?
first of all, read mongoid documentation relations!
mongoid embedded 1-1
one 1 relationships children embedded in parent document defined using mongoid's embeds_one , embedded_in macros.
mongoid refrence 1-1
one 1 relationships children referenced in parent document defined using mongoid's has_one , belongs_to macros.
from mongodb documentation :
embeds_one
benefits against has_one
- generally better performance read operations.
- the ability request , retrieve related data in single database operation.
large data problem :
embedding related data in documents, can lead situations documents grow after creation. document growth can impact write performance , lead data fragmentation. furthermore, documents in mongodb must smaller maximum bson document size.
that happens when using embeds_many
.
embeds_one
embeds model inside model has_one
save reference in model.
mongodb save document in database way (from mongodb examples)
reference 1-1 :
{ _id: "joe", name: "joe bookreader" } { patron_id: "joe", street: "123 fake street", city: "faketon", state: "ma" zip: 12345 }
embedded 1-1
{ _id: "joe", name: "joe bookreader", address: { street: "123 fake street", city: "faketon", state: "ma" zip: 12345 } }
Comments
Post a Comment