我是 mongodb NoSQL概念的新手,并且停留在我无法决定对最适合我的目的的模式进行建模的地方。
我需要以这样一种方式设计架构,使我的最终结果为Posts 和 Shares 按时间排序。为此,我考虑了两种选择:
选项 1:帖子和共享的不同集合:
帖子集合的架构:
var postSchema = mongoose.Schema({
postText: String,
postedBy: String,
privacy: Number,
updatedOn: { type: Date, default: Date.now }
}, { collection: 'posts' });
共享集合模式
var shareSchema = mongoose.Schema({
dis_Id: { type: mongoose.Schema.Types.ObjectId }, // Id of post that is shared
shareBy: { type: mongoose.Schema.Types.ObjectId },
shareText: String,
share_privacy: Number,
shareOn: { type: Date, default: Date.now }
}, { collection: 'shares' });
选项 2:在帖子本身中嵌入分享
Post 的新架构
var postSchema = mongoose.Schema({
postText: String,
postedBy: String,
updatedOn: { type: Date, default: Date.now },
privacy: Number,
share: {
shareBy: { type: mongoose.Schema.Types.ObjectId },
shareText: String,
share_privacy: Number,
shareOn: { type: Date }
}
}, { collection: 'posts' });
现在哪一个可能是更好的选择?选项 1 在查询中存在问题,因为 mongodb 中没有连接,而选项 2 将导致相同数据的复制,并且可以为数十万用户增长到数十亿以上。