【发布时间】:2019-04-04 18:20:08
【问题描述】:
我想在我的数据库中插入一个网站表单中的文档。我有一个用 mongoose 创建的模型,我只想将包含数据的属性保存在数据库中,我不想保存空属性。
这是我的模型:
const localizationSchema = new Schema({
name: { type: String, required: true },
spins: [{ type: String }],
spinsForChild: [{ type: String }],
parent: { id: String, name: String },
localizationType: { type: String },
count: { type: Number, default: 0 },
countries: [{ id: String, name: String, cities: [{ id: String, name: String }] }]
});
const Localization = mongoose.model('Localization', localizationSchema);
当我尝试保存一个新文档时,它会在数据库中创建所有属性,尽管我没有在我的查询中发送它。
Localization.create({
name: body.name,
localizationType: body.localizationType,
"parent.id": parent.id,
"parent.name": parent.name,
spins: spins,
spinsForChild: spinsForChild
}, function(err) {
if (err) return handleError(err);
res.redirect('/localizations');
});
例如,此代码在数据库中插入一个名为“国家”的空数组。
我尝试在模型声明中使用 strict: false 但它不起作用。
【问题讨论】: