Mongoose子文档与嵌套模式 [英] Mongoose subdocuments vs nested schema
问题描述
我很好奇在我的主模式中使用子文档与更深层的利弊:
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]
});
或
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.
推荐答案
根据文档,它完全一样。
但是,使用Schema会添加一个 _id
字段(只要你没有禁用它),并且可能会使用更多资源进行跟踪subdocs。
According to the docs, it's exactly the same.
However, using a Schema would add an _id
field as well (as long as you don't have that disabled), and presumably uses some more resources for tracking subdocs.
备用声明语法
v3中的新功能如果您不需要访问子文档模式实例,您也可以通过简单地传递对象文字[...]
New in v3 If you don't need access to the sub-document schema instance, you may also declare sub-docs by simply passing an object literal [...]
这篇关于Mongoose子文档与嵌套模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!