• 将出版者文档嵌入到书籍文档中将导致出版者数据重复

    image.png

    • 为避免重复出版商数据,请使用引用
    • 使用引用时,关系的增长决定了将引用存储在何处。 如果每个出版商的图书数量很少且增长有限,则有时将图书参考存储在出版商文档中可能会很有用。 否则,如果每个发布者的书籍数量不受限制,则此数据模型将导致可变的,不断增长的数组,如以下示例所示 ```json { name: “O’Reilly Media”, founded: 1980, location: “CA”, books: [123456789, 234567890, …] }

    { _id: 123456789, title: “MongoDB: The Definitive Guide”, author: [ “Kristina Chodorow”, “Mike Dirolf” ], published_date: ISODate(“2010-09-24”), pages: 216, language: “English” }

    { _id: 234567890, title: “50 Tips and Tricks for MongoDB Developer”, author: “Kristina Chodorow”, published_date: ISODate(“2011-05-06”), pages: 68, language: “English” }

    1. - 为了避免可变的,增长的数组,请将出版商参考存储在书籍文档中
    2. ```json
    3. {
    4. _id: "oreilly",
    5. name: "O'Reilly Media",
    6. founded: 1980,
    7. location: "CA"
    8. }
    9. {
    10. _id: 123456789,
    11. title: "MongoDB: The Definitive Guide",
    12. author: [ "Kristina Chodorow", "Mike Dirolf" ],
    13. published_date: ISODate("2010-09-24"),
    14. pages: 216,
    15. language: "English",
    16. publisher_id: "oreilly"
    17. }
    18. {
    19. _id: 234567890,
    20. title: "50 Tips and Tricks for MongoDB Developer",
    21. author: "Kristina Chodorow",
    22. published_date: ISODate("2011-05-06"),
    23. pages: 68,
    24. language: "English",
    25. publisher_id: "oreilly"
    26. }