typegoose: @modelOptions() in SubDocument not correct work

@modelOptions({ schemaOptions: { _id: false } }) - this options does not work

If you use @modelOptions ({schemaOptions: {_id: false}}) for a sub-document model, all added sub-documents still have _id

Code Example

@modelOptions({ schemaOptions: { _id: false } }) // this model options not work
export class SubDocument{
	@prop({ type: Number, min: 1, max: 5 })
	id!: number;
	@prop({ type: Number, min: 0, max: 4 })
	position!: number;
}

@plugin(MongooseAutoIncrementID.plugin, { modelName: 'UserUnit', field: 'id' })
@modelOptions({ schemaOptions: { collection: 'user_unit' } }) // this model option work correctly
export class UserUnit {
	@prop({ type: Number })
	id!: number;
	@prop({ type: Number, required: true })
	userId!: number;
	@arrayProp({ items: SubDocument})
	subDocument!: Types.Array<SubDocument>;
}

const unit  = await UserUnitModel.findOne({id});
const id = 1;
const position = 2;
unit.items.addToSet({id, position});
await unit.save();

console.log(unit.items);
/**
 [
   {
     "_id": "5dda88124ccc7d420c88fab2",
     "id":  1,
     "position": 2
   }
]

*/

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 26 (13 by maintainers)

Most upvoted comments

@nasr18 like i said i never used mongoose-auto-increment so i dont really know how to use it -> but from what i get from the documentation, the steps are:

  1. connect to database
  2. initialize the plugin with mai.initalize(mongoose.connection)
  3. create the models