Search code examples
javascripttypescriptmongoosetypesmongoose-schema

What should be the type of a field in schema if "any" cannot be used in Nest.js?


I'm working in Nest.js using Mongoose. While creating schema, I have a field extra_options which can store any type of value (array, object, string etc). Keeping its type as "any" does not work. What should be the correct type? Here is a fragment of the code.

@Schema({
    collection: 'xyz',
    timestamps: {
        updatedAt: 'updated_at',
        createdAt: 'created_at'
    },
})
export class xyz {
    @Prop({default: true})
    active: boolean;

    @Prop()
    extra_options: any;

    @Prop({required: true})
    created_by: string;
}

Solution

  • import * as mongoose from 'mongoose';
    
    @Prop({type: mongoose.Schema.Types.Mixed})
    extra_options: any;
    

    This worked.