参考映射和嵌入映射之间的区别

问题描述 投票:0回答:1

Reference MappingEmbedded MappingDoctrine MongoDB ODM有什么区别?

我只需要实现一对多的关系。

mongodb doctrine-orm
1个回答
1
投票

嵌入的文档存储在文档本身中。参考文件存储在别处。

参考文档如何存储在db中的简化示例:

//collection one
{
   _id: "one_1"
   many: [
       "many_1",
       "many_2",
       "many_3"
   ]
 }

 //collection many
 {
     _id: "many_1",
     name: "one"
 },
 {
     _id: "many_2",
     name: "two"
 },
 {
     _id: "many_3",
     name: "three"
 }

和嵌入式文件:

//collection one
{
   _id: "one_1"
   many: [
       { _id: "many_1", name: "one"},
       { _id: "many_2", name: "two"},
       { _id: "many_3", name: "three"}
   ]
 }

前者更灵活,后者更快。如果您需要单独修改子文档,或者如果您需要多对多参考,或者如果您在可预见的未来由于大量嵌入式文档而达到16MB大小限制,则经验法则是使用引用。

© www.soinside.com 2019 - 2024. All rights reserved.