ruby on rails - What exactly is the difference between has_many, has_and_belongs_to_many and embeds_many in mongoid? -
i understand not programming problem, unable find clear , descriptive solution.
mongoid's documentation quite clear:
embedded relations describe documents stored inside other documents in database.
referenced relations describe documents reference documents in collection storing foreign key data (usually id) other document in itself.
in detail:
referenced 1-n / has_many
when defining relation of nature, each document stored in respective collection, child document contains "foreign key" reference parent.
# parent band document. { "_id" : objectid("4d3ed089fb60ab534684b7e9") } # child member document. { "_id" : objectid("4d3ed089fb60ab534684b7f1"), "band_id" : objectid("4d3ed089fb60ab534684b7e9") }
referenced n-n / has_and_belongs_to_many
when defining relation of nature, each document stored in respective collection, , each document contains "foreign key" reference other in form of array.
# band document. { "_id" : objectid("4d3ed089fb60ab534684b7e9"), "tag_ids" : [ objectid("4d3ed089fb60ab534684b7f2") ] } # tag document. { "_id" : objectid("4d3ed089fb60ab534684b7f2"), "band_ids" : [ objectid("4d3ed089fb60ab534684b7e9") ] }
embedded 1-n / embeds_many
documents embedded using embeds_many
macro stored array of hashes inside parent in parent's database collection.
{ "_id" : objectid("4d3ed089fb60ab534684b7e9"), "albums" : [ { "_id" : objectid("4d3ed089fb60ab534684b7e0"), "name" : "violator", } ] }
Comments
Post a Comment