使用Mongoose填充和直接对象包含之间是否存在任何性能差异(查询的处理时间)?什么时候应该使用?
猫鼬人口例子:
var personSchema = Schema({
_id : Number,
name : String,
stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = Schema({
_creator : { type: Number, ref: 'Person' },
title : String,
});
Run Code Online (Sandbox Code Playgroud)
Mongoose对象嵌套示例:
var personSchema = Schema({
_id : Number,
name : String,
stories : [storySchema]
});
var storySchema = Schema({
_creator : personSchema,
title : String,
});
Run Code Online (Sandbox Code Playgroud) 架构(子文档)中的嵌套架构与创建两个单独的模型并引用它们之间有什么区别,它们的性能如何?
子文档:
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子文档和嵌套模式完全相同.但是嵌套模型在数据库中创建单独的集合.我的问题是嵌套模式与嵌套模型的差异是什么,而不是子文档与嵌套模式.