对猫鼬/蒙哥语术语感到困惑.子文档/嵌入式文档也是集合吗? [英] Confused about Mongoose/Mongo Terminology. Are Sub-Docs/Embedded-Docs also Collections?

查看:59
本文介绍了对猫鼬/蒙哥语术语感到困惑.子文档/嵌入式文档也是集合吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下猫鼬模型:

If I have the following mongoose models:

// child.model.js
var mongoose      = require('mongoose'),
    Schema        = mongoose.Schema,
    childSchema   = new Schema({

        name: String,
        age: Number

    }, {collection: 'children'});

module.exports = mongoose.model('Child', childSchema);

//parent.model.js
var mongoose      = require('mongoose'),
    Child         = require('./child.model.js'),
    Schema        = mongoose.Schema,
    parentSchema  = new Schema({

        name: String,
        children: [Child.schema]

    });

module.exports = mongoose.model('Parent', parentSchema);

这是否意味着我将拥有两个收藏,一个叫做父母",另一个叫做孩子"?

Would this mean I would then have two collections, one called 'parents' and one called 'children'?

据我目前的理解,以上代码创建了一个嵌套的文档结构,其中Child对象仅存在于Parent文档的集合中.但是,我对可以传递给Schema构造函数的{collection: 'name'}选项感到困惑.创建这样的子文档时,是否仅忽略此选项?

As I currently understand it the above code creates a nested document structure whereby the Child objects exist only within the collection of Parent documents. However, I'm getting confused by the {collection: 'name'} option that you can pass to the Schema constructor. Is this option just ignored when creating sub-documents like this?

推荐答案

有两种子文档-参考.这是猫鼬级别的分类.在MongoDB级别上,它只是集合

There are two kinds of subdocs - Embedded and Referenced. This is a Mongoose-level classification. At MongoDB level it's just Collections and Documents.

Mongoose中嵌入式文档和引用文档之间的区别在于,前者类似于将子架构嵌入"在父级中. IE.就MongoDB而言,它(父)只是一个大文件.

The difference between Embedded and Referenced docs in Mongoose is that the former is akin to having the child schema "embedded" in the parent. I.e. as far as MongoDB is concerned it (Parent) is just a one big document.

在引用文档中,父文档仅存储子文档的ObjectID,即子文档被引用",并留给您"

Whereas in referenced docs the Parent document only stores the child document's ObjectID, i.e. the child document is "referenced", and it's left up to you to "populate" the entire document.

您使用的children: [Child.schema]是嵌入式文档的语法.

What you're using children: [Child.schema] is the syntax of an Embedded document.

这是否意味着我将拥有两个收藏,一个叫做父母",另一个叫做孩子"?

Would this mean I would then have two collections, one called 'parents' and one called 'children'?

所以您在MongoDB中将只有1个集合.

So you'll have only 1 collection in MongoDB.

但是,我对可以传递给Schema构造函数的{collection:'name'}选项感到困惑.创建这样的子文档时,是否仅忽略此选项?

However, I'm getting confused by the {collection: 'name'} option that you can pass to the Schema constructor. Is this option just ignored when creating sub-documents like this?

该选项只是这样,如果您实际上是要从中创建模型模式,它使用您提供的名称,而不是自动推断.

That option is just so that if you were to actually create a model from that schema, it uses the name you provided instead of automatically inferring.

这篇关于对猫鼬/蒙哥语术语感到困惑.子文档/嵌入式文档也是集合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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