I am using mongodb (mongoose) in my nest js project. I created this schema that i used for authentication:
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
export type AuthenticationDocument = Authentication & Document;
@Schema()
export class Authentication {
@Prop({ required: true })
email: string;
@Prop({ required: true })
password: string;
@Prop()
isActive: boolean;
@Prop()
userType: string;
}
export const AuthenticationSchema =
SchemaFactory.createForClass(Authentication);
Everything is working.
This schema is used when user wants to register or login.
. This schema generates in mongodb something like this:
_id:eidfywieu6r88ey9ew
password:$2b$10$BdQIK/EdDC6/UMNlE/Awye48AFS8Vcpecm6lOixhl4zTLkb0EIdIW
email:test@test.com
__v:0
Question: If i want to add additional field in my monngodb for example when user does additional action. Could i add additional field in mongodb like:
_id:eidfywieu6r88ey9ew
password:$2b$10$BdQIK/EdDC6/UMNlE/Awye48AFS8Vcpecm6lOixhl4zTLkb0EIdIW
email:test@test.com
code: test code // added field
__v:0
Or i should initiate the code field when user is registered the first time and to change the schema like
@Schema()
export class Authentication {
@Prop({
required: true
})
email: string;
@Prop({
required: true
})
password: string;
@Prop()
isActive: boolean;
@Prop()
userType: string;
@Prop()
code: null;
}
? Which is the best solution?