【问题标题】:NoSQL duplicate data or new documentNoSQL 重复数据或新文档
【发布时间】:2017-12-10 11:02:35
【问题描述】:

我正在搞乱 MongoDB,我想知道最好的解决方案是什么:我有一个动物集合,每个动物都有一个名称和一个类型。

但是对于前端需求,每种类型都有一种颜色。

所以有两种可能:

第一个:

Animal
  .animal 1
    name: Simba
    race: lion
    type:
        name: felin
        color: blue
  .animal 2
    name: Franck
    race: turtle
    type:
        name: turtle
        color: red
  .animal 3
    name: Francis
    race: turtle
    type:
        name: turtle
        color: red

第二:

Animal
  .animal 1
    name: Simba
    race: lion
    type: (id of type collection)
Type
  .type 1
    name: felin
    color: blue
  .type 2
    name: turtle
    color: red

那么,我应该复制数据吗?还是我应该创建一个新集合?

感谢您的宝贵时间!

【问题讨论】:

    标签: mongodb performance nosql


    【解决方案1】:

    如果您的 type 文档不会经常更改并且仅限于其中的 2-3 个属性,则第一种方法会更好。

    但是,如果您打算在 type 文档中添加更多属性,那么您应该考虑创建一个新集合并将 type-id 存储在 animals 集合中。

    另外,如果您的 type 文档将来会频繁更改,您不想更新 animals 中包含 type 的每条记录,而是只想在 1 处更改它地点(type 收藏)。

    【讨论】:

      【解决方案2】:

      我更喜欢第一种方法,因为类型键只有 2 个子键。当我有更多的键时,我更喜欢新的集合,因为它涉及数据库操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-24
        • 2011-12-23
        • 1970-01-01
        • 2014-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-09
        相关资源
        最近更新 更多