【问题标题】:Determine the data structure确定数据结构
【发布时间】:2019-11-24 05:48:07
【问题描述】:

我是 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
});

【问题讨论】:

  • 看到mongo接受json结构,所以你只需要弄清楚你已经拥有的模型,现在用你选择的后端语言创建模型类,选择一个ORM层然后推送模型到 mongo 服务器。
  • 我明白了。我需要知道我的数据结构是否正确。
  • 您能描述一下您有哪些不同的数据访问模式吗?您对当前模型有什么担忧?
  • 我想知道好不好。
  • 你能不能好心地举一个你的数据结构的 JSON 示例?

标签: database mongodb mongoose data-structures mongoose-schema


【解决方案1】:

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

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

在这两种情况下,我们都必须查找详细信息。但优势将是读取性能,因此我们在单个集合对象中存储的数据更少。根对象中的数据越多,查询响应速度就越慢。

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
            },
        }
    ]
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多