【发布时间】:2016-06-07 12:55:36
【问题描述】:
正在学习如何使用 mongoose,并且正在尝试设计可靠可变的架构。该应用程序将发布到不同的服务(例如 Twitter、Tumblr)并将它们存储在一个集合中(“帖子”)。会有一些共同点(例如发布时间或简短摘要),但其他领域(例如帖子内容、博客帖子的随附脚本)会有所不同。
有什么好的方法来解决这个问题?有没有一种好方法可以将不同的集合绑定在一起来避免这种情况?参考/子模式?使用Schema.Types.Mixed,并通过使用安全检查扩展默认方法来加强一致性?
// Example pseudo-functioning schemas
const tweetSchema = new mongoose.Schema({
tweetUrl: {type: string, trim: true}
length: Number
});
const blogSchema = new mongoose.Schema({
title: String,
edits: [Date],
slug: { type: String, trim: true},
body: String
});
const postSchema = new mongoose.Schema({
published: Date,
summary: String,
type: String,
contents: blogSchema || tweetSchema
});
【问题讨论】: