0

我正在使用 JavaScript(没有任何框架)创建一个仅限客户端的应用程序,将使用 MongoDB 来存储数据。我可以想到2 种方法来建模我的数据。

有人可以帮我理解哪个更合适。

  • 方式#1

    [{
        title: "bucketList",
        id: 1,
        tasks: [{
            title: "play soccer for world league",
            id: 1,
            done: false,
            comments: ["fifa 2014 is about to start", "need to go buy a Brazil T-shirt"]
        }, {
            title: "start a school",
            id: 2,
            done: true,
            comments: ["start with being a mentor"]
        }]
    }, {
        title: "to-do",
        id: 2,
        tasks: [{
            title: "create a todo App",
            id: 1,
            done: false,
            comments: []
        }, {
            title: "watch GOT",
            id: 2,
            done: false,
            comments: ["whitewalkers seems to be in no hurry"]
        }]
    }]
    
  • 方式#2

    [{
        collection - title: "bucketList",
        collection - id: 1,
        title: "play soccer for world league",
        id: 1,
        done: false,
        comments: ["fifa 2014 is about to start", "need to go buy a Brazil T-shirt"]
    }, {
        collection - title: "bucketList",
        collection - id: 1,
        title: "start a school",
        id: 2,
        done: true,
        comments: ["start with being a mentor"]
    }, {
        collection - title: "to-do",
        collection - id: 2,
        title: "create a todo App",
        id: 1,
        done: false,
        comments: []
    }, {
        collection - title: "to-do",
        collection - id: 2,
        title: "watch GOT",
        id: 2,
        done: false,
        comments: ["whitewalkers seems to be in no hurry"]
    
    }] 
    
4

1 回答 1

0

我的两分钱是:

我实际上取决于你想如何处理你的数据。

方式#1 您可以一次管理整个待办事项列表,其中包含许多待办事项方式#2 让您可以直接访问每个待办事项。

way#1 可以很容易地从一个列表中访问所有待办事项,但是例如更新“完成”将需要更多的工作,因为它是一个数组。

way#2 使“完成”的更新更容易,但您需要一些迭代/聚合来查找列表的所有操作。

way#2 对我来说似乎更容易处理,因为例如完成的更新更容易处理,而包含多个文档的查找更容易处理。还要考虑你想对数据做什么,例如,如果你有一个截止日期,并且你想在所有待办事项列表中显示所有到期任务,方式#2 将是 def。更好的选择。

于 2014-06-09T09:44:57.200 回答