我知道这不是编程问题,我找不到非常清晰和描述性的解决方案。
最佳答案
Mongoid 的 documentation很清楚:
Embedded relations describe documents who are stored inside other documents in the database.
Referenced relations describe documents that reference documents in another collection by storing foreign key data (usually an id) about the other document in itself.
详细说明:
引用 1-n/has_many
定义这种性质的关系时,每个文档都存储在其各自的集合中,但子文档包含对父文档的“外键”引用。
# The parent band document.
{ "_id" : ObjectId("4d3ed089fb60ab534684b7e9") }
# The child member document.
{
"_id" : ObjectId("4d3ed089fb60ab534684b7f1"),
"band_id" : ObjectId("4d3ed089fb60ab534684b7e9")
}
引用 n-n/has_and_belongs_to_many
当定义这种性质的关系时,每个文档都存储在其各自的集合中,并且每个文档都包含一个以数组形式引用另一个文档的“外键”。
# The band document.
{
"_id" : ObjectId("4d3ed089fb60ab534684b7e9"),
"tag_ids" : [ ObjectId("4d3ed089fb60ab534684b7f2") ]
}
# The tag document.
{
"_id" : ObjectId("4d3ed089fb60ab534684b7f2"),
"band_ids" : [ ObjectId("4d3ed089fb60ab534684b7e9") ]
}
嵌入式 1-n/embeds_many
使用 embeds_many
宏嵌入的文档作为散列数组存储在父级数据库集合中的父级中。
{
"_id" : ObjectId("4d3ed089fb60ab534684b7e9"),
"albums" : [
{
"_id" : ObjectId("4d3ed089fb60ab534684b7e0"),
"name" : "Violator",
}
]
}
关于ruby-on-rails - mongoid中的has_many、has_and_belongs_to_many和embeds_many到底有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18488193/