我该如何为JS应用程序建模我的数据

时间:2014-06-09 08:39:52

标签: javascript mongodb model db-schema

我使用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"]
    
    }] 
    

1 个答案:

答案 0 :(得分:0)

我的两个便士是:

我实际上取决于您希望如何处理数据。

#p>方式#1你用许多do dos一次管理一个完整的do do列表 方式#2让你可以直接访问每个人。

方式#1可以轻松地从一个列表访问所有待办事项,但更新“完成”例如将是一个更多的工作,因为它是一个数组。

方式#2使“完成”的更新变得更容易,但是你需要一些迭代/聚合来查找列表的所有内容。

方式#2似乎更容易处理,因为例如更新完成后更容易处理,而具有多个文档的查找很容易处理。 还要考虑您要对数据执行的操作,例如,如果您有截止日期,并且希望在所有待办事项列表中显示所有到期任务,则方式#2将为def。更好的选择。

相关问题