首页 > 解决方案 > 创建嵌套模型的最佳实践是什么

问题描述

我是 mongoDB 和 mongoose 的新手,熟悉非常基本的模型 Schema。我遇到的问题是如何创建与动态键链接的嵌套对象。这是我想要的数据结构。如您所见,tasks 对象中的每个任务都有与自己的 id 相同的动态键。columns 具有相同的结构,columnOrder 同时存储了该列的 id。如何处理这种数据结构?我想我应该使用 objectId ref,但不确定。

export const fakeData = {
  tasks: {
    "task-1": { id: "task-1", content: "take out the garbage" },
    "task-2": { id: "task-2", content: "Watch movie" },
    "task-3": { id: "task-3", content: "Catch my phone" },
    "task-4": { id: "task-4", content: "cookDinner" }
  },
  columns: {
    "column-1": {
      id: "column-1",
      title: "To do",
      taskIds: ["task-1", "task-2", "task-3", "task-4"]
    },
    "column-2": {
      id: "column-2",
      title: "In progress",
      taskIds: []
    },
    "column-3": {
      id: "column-3",
      title: "Done",
      taskIds: []
    }
  },
  columnOrder: ["column-1", "column-2", "column-3"]
};

我真的很感谢有人教我如何处理这个问题。

标签: mongodbmongoose

解决方案


推荐阅读