【发布时间】:2017-07-06 12:35:15
【问题描述】:
在模式(子文档)中嵌套模式与创建两个单独的模型并引用它们有什么区别,它们的性能如何?
子文档:
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 - mongoose 子文档和嵌套模式完全一样。 但是嵌套模型在数据库中创建了一个单独的集合。 我的问题是嵌套模式与嵌套模型的区别是什么,而不是子文档与嵌套模式。
【问题讨论】:
-
那个 dupe q/a 不直接解决单独的模型,只是嵌入模式。这肯定已经被问过了......
-
您可以使用 Mongoose 术语改进问题,即“通过引用填充”