# MongoDB关联查询

**当设计数据表，需要往数据表中添加新的列的时候，只需要更改集合中的Schema模型，然后对原来的信息进行挨个的修改**

* 1.首先来说说这个Schema的设计的问题
  * 在数据类型上就分为以下几种
    * [String](https://mongoosejs.com/docs/schematypes.html#strings)
    * [Number](https://mongoosejs.com/docs/schematypes.html#numbers)
    * [Date](https://mongoosejs.com/docs/schematypes.html#dates)
    * [Buffer](https://mongoosejs.com/docs/schematypes.html#buffers)
    * [Boolean](https://mongoosejs.com/docs/schematypes.html#booleans)
    * [Mixed](https://mongoosejs.com/docs/schematypes.html#mixed)
    * [ObjectId](https://mongoosejs.com/docs/schematypes.html#objectids)
    * [Array](https://mongoosejs.com/docs/schematypes.html#arrays)
    * [Decimal128](https://mongoosejs.com/docs/api.html#mongoose_Mongoose-Decimal128)
    * [Map](https://mongoosejs.com/docs/schematypes.html#maps)
  * 所有数据类型都有的选项，还有一些只有某些数据类型才有的选项，具体可参考官方网址

    * `required`: boolean or function, if true adds a [required validator](https://mongoosejs.com/docs/validation.html#built-in-validators) for this property
    * `default`: Any or function, sets a default value for the path. If the value is a function, the return value of the function is used as the default.
    * `select`: boolean, specifies default [projections](https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/) for queries
    * `validate`: function, adds a [validator function](https://mongoosejs.com/docs/validation.html#built-in-validators) for this property
    * `get`: function, defines a custom getter for this property using [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty).
    * `set`: function, defines a custom setter for this property using [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty).
    * `alias`: string, mongoose >= 4.10.0 only. Defines a [virtual](https://mongoosejs.com/docs/guide.html#virtuals) with the given name that gets/sets this path.

    ```javascript
    /*
    定义教师表结构
     */
    var TeacherSchema=new Schema({
        name:{
            type: String,
            required: true
        },
        position:{
            type: String,
            required: true,
            default:"教授"
        }
    })
    ```

    * **在这个数据类型设计的时候我犯了两个错误**

    ​ 1.required少打了，打成require

    ​ 2.default生效的情况是在初始化一个实例对象的时候就没有，申明position字段才会将默认值填充到数据之中
* 2.多表进行关联查询

  参考 <https://www.jianshu.com/p/817ff51bd548>
