database - 确定数据结构

标签 database mongodb mongoose data-structures mongoose-schema

我是 Mongo 的新手。请帮助确定数据结构。我有分支,每个分支都有名称和屏幕数,每个分支都可以有很多播放列表,每个播放列表都有名称、开始日期、结束日期、总时间和文件。我需要告诉每个文件在哪个屏幕上应该以什么顺序显示,以及显示时间。我想在具有不同属性的不同播放列表中使用一个文件

    const FileSchema = Schema({
    url: {
        type: String,
        required: true
    },
    showTime: {
        type: Date,
        required: true
    },
    screen: {
        type: Number,
        required: true
    },
    order: {
        type: Number,
        required: true
    },
}, {
    timestamps: true
});

const PlaylistSchema = Schema({
    name: {
        type: String,
        required: true
    },
    endDate: {
        type: Date,
        required: true
    },
    files: [FileSchema]
}, {
    timestamps: true
});

const BranchSchema = Schema({
    name: {
        type: String,
        required: true
    },
    screens: {
        type: Number,
        required: true
    },
    playlists: [Playlists]
}, {
    timestamps: true
});

最佳答案

处理多层多对多关系总是让人头疼。 设计像关系数据库这样的东西并使用查找来管理所有东西是个好主意。

如果我们从集合 playlistsfiles 中删除一个 id 数组并添加对子节点的引用,这将更有意义。

在这两种情况下,我们都必须查找详细信息。但 advantag 将提高读取性能,因此我们在单个集合对象中存储的数据较少。根对象中的更多数据会减慢查询响应。

const FileSchema = Schema({
    playlistId: Schema.Types.ObjectId,
    url: {
        type: String,
        required: true
    },
    showTime: {
        type: Date,
        required: true
    },
    screen: {
        type: Number,
        required: true
    },
    order: {
        type: Number,
        required: true
    },
}, {
    timestamps: true
});

const PlaylistSchema = Schema({
    branchId: Schema.Types.ObjectId,
    name: {
        type: String,
        required: true
    },
    endDate: {
        type: Date,
        required: true
    }
}, {
    timestamps: true
});

const BranchSchema = Schema({
    name: {
        type: String,
        required: true
    },
    screens: {
        type: Number,
        required: true
    },
}, {
    timestamps: true
});

如果您不想要很多集合并且您的 PlaylistSchema 不包含太多属性,您可以将其设为 BranchSchema 中的对象数组并使用其 _id 字段管理 FileSchema

const BranchSchema = Schema({
    name: {
        type: String,
        required: true
    },
    screens: {
        type: Number,
        required: true
    },
    branches: [
        {

            name: {
                type: String,
                required: true
            },
            screens: {
                type: Number,
                required: true
            },
        }
    ]
});

关于database - 确定数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57041192/

相关文章:

android - 在 ViewFlipper 中显示图像数据库

sql-server - 系统进程正在锁定我的数据库文件 MDF

node.js - 如何在嵌套对象的嵌套数组中设置属性未定义

javascript - Node.js,异步和 if 语句

php - 多次使用 LastInsertId,PHP PDO

php - 当项目在指定列中具有相同值时合并 SQL 数据库值

Parse Server 应用程序的 MongoDB 索引

mongodb - 拉推原子操作?

mongodb - 在Mongodb中,如何检索符合条件的对象的子集?

mysql - 如何在数据库中存储可版本化的数据?