4
模式中的嵌套模式(子文档)与创建两个单独模型并引用它们之间的区别是什么?它们的性能如何?Mongoose嵌套模式vs嵌套模型
子文档:
const postSchema = new Schema({
title: String,
content: String
});
const userSchema = new Schema({
name: String,
posts: [postSchema]
});
module.export = mongoose.model('User', userSchema);
嵌套模型(通过参考填充):
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);
编辑:这不是一个重复的问题。
在这个问题中:Mongoose subdocuments vs nested schema - 猫鼬子文档和嵌套模式是完全一样的。 但嵌套模型在数据库中创建一个单独的集合。 我的问题是什么是嵌套架构vs嵌套模型差异,而不是子文档vs嵌套架构。
这欺骗Q/A并不直接针对单独的模型,只是嵌入式架构中引用它们。这个问题已经被问过了...... – Matt
您可以通过使用Mongoose术语来改善问题,即“通过引用填充” – Paul