Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Web Technology by (50.2k points)

I'm curious as to the pros and cons of using subdocuments vs a deeper layer in my main schema:

var subDoc = new Schema({

  name: String

});

var mainDoc = new Schema({

  names: [subDoc]

});

or

var mainDoc = new Schema({

  names: [{

    name: String

 }]

});

I'm currently using subdocs everywhere but I am wondering primarily about performance or querying issues I might encounter.

1 Answer

0 votes
by (108k points)

In Mongoose, the subdocuments are considered to be those documents that are embedded inside the other documents. The instance that you have shown, both are implying the same thing that is embedded documents. There are no pros and cons regarding the subdocuments and nested documents. The nested schema is very similar to the top-level schemas as this nested schema comprises middleware, custom-validation-logic, and virtuals. The subdocuments are incapable of saving individual documents. Instead, they are saved when their top-level parent document is saved.  

You can refer to this documentation if you want to get a detailed explanation regarding the same. 

For more clarity, you can check out this MongoDB Training Course.

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer

Browse Categories

...