What is the difference between the nesting scheme in the scheme (subdocuments) and the creation of two separate models and a reference to them, but how is their effectiveness?
attached documents:
const postSchema = new Schema({ title: String, content: String }); const userSchema = new Schema({ name: String, posts: [postSchema] }); module.export = mongoose.model('User', userSchema);
nested models (filling in by reference):
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);
Edit: This is not a duplicate question.
In this question: Mongoose substrates versus nested schema - mongoose subdocuments and nested schema exactly match. BUT nested models create a separate collection in the database. My question is what is the difference in nested schema and nested models, rather than nested documents with nested schema.
source share