Mongoose 子文档与嵌套模式 [英] Mongoose subdocuments vs nested schema

查看:37
本文介绍了Mongoose 子文档与嵌套模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇在我的主架构中使用子文档与使用更深层的利弊:

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
 }]
});

我目前在任何地方都使用 subdocs,但我主要想知道我可能遇到的性能或查询问题.

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

推荐答案

根据文档,完全一样.但是,使用架构也会添加一个 _id 字段(只要您没有禁用该字段),并且可能会使用更多资源来跟踪子文档.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆