参考文档中的混合类型

时间:2018-08-13 16:55:30

标签: mongodb mongoose

我的应用程序中的用户可以创建项目任务 Tasks 可以是单独的,也可以在 Project 中,该项目是 Tasks 的容器。

项目

const projectSchema = new Schema({
    title: { type: String, required: true },
    description: { type: String, required: true },
    authorId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
});

任务

const taskSchema = new Schema({
    name: { type: String, required: true },
    description: { type: String, required: true },
    projectId: { type: mongoose.Schema.Types.ObjectId, ref: "Project", required: false },
    authorId: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
});

这是一种社交平台,用户可以在其时间线上共享整个 Project ,该 Project 中的一个 Task 或一个单个 Task 。嵌入在帖子中,也可以只是文本而没有其他内容。

发布

const postSchema = new Schema({
    content: { type: String, required: false },
    authorId: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
    projectId: { type: mongoose.Schema.Types.ObjectId, ref: "Project", required: false } },
    taskId: { type: mongoose.Schema.Types.ObjectId, ref: "Task", required: false } }
});

当然必须至少是某些东西,但是如果 User 选择嵌入 Task Project ,则必须只有其中之一。像这样:

const postSchema = new Schema({
    content: { type: String, required: false },
    authorId: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
    media: { type: mongoose.Schema.Types.ObjectId, ref: ["Project", "Task"], required: false } },
});

但是我想这根本不是一种好的语法,什么是使其有效的好方法?我也希望我的用户能够发布任何类型的媒体,不仅是 Task Project ,而且还可以发布 Images Videos 链接 ...

0 个答案:

没有答案
相关问题