docs.nestjs.com icon indicating copy to clipboard operation
docs.nestjs.com copied to clipboard

Mongoose population type is wrong

Open MickL opened this issue 3 years ago • 1 comments

I'm submitting a...

  • [ ] Regression
  • [ ] Bug report
  • [ ] Feature request
  • [X] Documentation issue or request (new chapter/page)
  • [ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

In docs of https://docs.nestjs.com/techniques/mongodb the population example is shown as the following:

@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Owner' })
owner: Owner;

But actually this is not correct, instead in reality owner is of type ObjectId. Only when explicitly populating the query owner ist of type Owner.

In the example the definition of CreateCatDto is not shown but if the types are used everywhere then the compiler would complain because there is a mismatch: When creating a new Cat one need to add owner of type ObjectId and not of type Owner as the compile would demand. Also when querying for Cat one will get an ObjectId while the compile thinks it is Owner.

Expected behavior

Provide a better example / best practice of population. Mongoose docs: https://mongoosejs.com/docs/typescript/populate.html

MickL avatar Nov 24 '22 15:11 MickL

Hi! I want this improvement to be done as well, I'll send a PR because I think this is important.

AxelDavid45 avatar Sep 28 '23 18:09 AxelDavid45