猫鼬默认设置为空对象 [英] Mongoose set default as empty object

查看:83
本文介绍了猫鼬默认设置为空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将star_info属性设置为对象类型(混合模式),并使用

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: { type : Schema.Types.Mixed, default : { }}

在数据库中,保存文档时没有字段star_info.如何获取mongoose设置默认值?

In the database, there is no field star_info when saving the documents. How do I get mongoose to set the default value?

推荐答案

默认情况下(为了最大程度地减少存储在MongoDB中的数据),Mongoose不会将空对象保存到数据库中.您可以通过在创建架构时将minimize标志设置为false来覆盖此行为.例如:

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

现在star_info将默认为空对象并保存到数据库.

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

有关更多信息,请参见 http://mongoosejs.com/docs/guide.html#minimize

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

这篇关于猫鼬默认设置为空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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