架构(子文档)中的嵌套架构与创建两个单独的模型并引用它们之间有什么区别,它们的性能如何?
子文档:
const postSchema = new Schema({
title: String,
content: String
});
const userSchema = new Schema({
name: String,
posts: [postSchema]
});
module.export = mongoose.model('User', userSchema);
Run Code Online (Sandbox Code Playgroud)
嵌套模型(按引用填充):
const postSchema = new Schema({
title: String,
content: String,
author: { type: String, ref: 'User' }
});
module.export = mongoose.model('Post', postSchema);
const userSchema = new Schema({
name: String,
posts: [{ type: Schema.Types.ObjectId, ref: 'Post'}]
});
module.export = mongoose.model('User', userSchema);
Run Code Online (Sandbox Code Playgroud)
编辑:这不是一个重复的问题.
在这个问题中:Mongoose子文档与嵌套模式 - mongoose子文档和嵌套模式完全相同.但是嵌套模型在数据库中创建单独的集合.我的问题是嵌套模式与嵌套模型的差异是什么,而不是子文档与嵌套模式.