Mongodb Typeorm:使用Mongo数据库为布尔值提供默认值
我正在使用Mongo数据库的typeorm。我想为数据类型为Mongodb Typeorm:使用Mongo数据库为布尔值提供默认值,mongodb,boolean,default-value,typeorm,Mongodb,Boolean,Default Value,Typeorm,我正在使用Mongo数据库的typeorm。我想为数据类型为boolean的列提供一个默认值 我的实体如下所示: @ObjectIdColumn() id: ObjectID; @Column() name: string; @Column() startDate: Date; @Column() endDate: Date; @Column() inspectionTypeId: string; @Column() questions: s
boolean
的列提供一个默认值
我的实体如下所示:
@ObjectIdColumn()
id: ObjectID;
@Column()
name: string;
@Column()
startDate: Date;
@Column()
endDate: Date;
@Column()
inspectionTypeId: string;
@Column()
questions: string[];
@Column('boolean', {default: true})
isActive: boolean;
但是,当我保存到回购中时,isActive
列不会被添加。试试这个
@Column({ type: 'boolean', default: true})
事实上,我也遇到过同样的问题。所以我的堆栈是相同的(MongoDB+TypeORM)。我在模型中有相同的
isActive
字段,我想做的是在默认情况下将其设置为“false”。我试图设置“false”,但未能达到目标
我重读了多次这部分()
默认值:字符串-添加数据库级列的默认值
我的想法是,default
选项根本不适用于布尔类型(也许我错了)
所以为了设置它,我使用了beforeensert
hook
@Column({
nullable: false,
select: false,
})
isActive: boolean;
@BeforeInsert()
beforeInsertActions() {
this.isActive = false;
}
有关挂钩的更多信息:您可以执行以下操作:
@Column('boolean', {default: true})
isActive: boolean = true;
如果不将值传递给isActive,它将为true,如果传递值,它将为该值。我只是将其用作:@Column({default:true})myField:boolean;而且效果很好。此外,该选项确实存在于TypeORM ColumnOptions中:这里可以看到官方示例用法,仅供参考:我目前正在使用带有postgres数据库的TypeORM v0.2.29,这个解决方案对我来说非常有效。我知道最初的问题是关于MongoDB的,但希望这能帮助其他偶然发现这条线索的人。