Search code examples
mongodbmongoosenestjsmongoose-schemanestjs-mongoose

What is the difference between Type[] and [Type] while defining mongoose entity Prop in nestjs


I am using following code to define a mongodb entity.

@Schema({})
export class User extends BaseModel {
    @Prop({ type: [String], required: true })
    subjects: string[]; // line 1
    // subjects: [string]; // line 2
  
}

What is the difference between line 1 and line 2 for defining a mongodb entity property?


Solution

  • According to the Mongoose SchemaTypes Documentation for Arrays, the type definition Type[] and [Type] are interchangeable. You could also use Array<Type>. They are essentially all the same internally.