Mongoose set default as empty object

前端 未结 2 1559
执笔经年
执笔经年 2020-12-29 02:04

I am trying to set the star_info attribute as an object type(Mixed Schema) and setting it\'s default value as an empty object using

star_info:          


        
2条回答
  •  天涯浪人
    2020-12-29 02:08

    By default (in an effort to minimize data stored in MongoDB), Mongoose will not save empty objects to your database. You can override this behavior by setting the minimize flag to false when you create your schema. For example:

    const schema = new Schema({
      star_info: { type: Schema.Types.Mixed, default: {} }
    }, { minimize: false })
    

    Now star_info will default to an empty object and save to the database.

    Read more at http://mongoosejs.com/docs/guide.html#minimize

提交回复
热议问题