Mongoose 6.4.X breaks PopulatedDoc parent-child relation typing, circular reference itself, works with 6.3.9.
Prerequisites
- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the bug has not already been reported
Mongoose version
6.4.9
Node.js version
16.15.1
MongoDB server version
5.0.3
Description
After upgrading from 6.3.9 to 6.4.6 my schemas are broken. I am using Nest Framework, but it's only decorators (see snippet below).
I tried to use the documentation but the documentation code is broken. https://mongoosejs.com/docs/typescript/populate.html#using-populateddoc ObjectId comes from nowhere, so the code can't run since ObjectId does not exist.
I tried everything I could without knowing what do to. At the moment I downgraded to 6.3.9 and it works. I am using TypeScript 4.7.4.


Steps to Reproduce
import { Prop, Schema } from "@nestjs/mongoose"; // Can safely be removed
import { Document, PopulatedDoc, SchemaTypes } from "mongoose";
export type ChildDocument = Child & Document;
export type ParentDocument = Parent & Document;
@Schema() // Can safely be removed
export class Child {
@Prop({ type: SchemaTypes.ObjectId, ref: 'Parent' }) // Can safely be removed
parent: PopulatedDoc<ParentDocument>;
}
@Schema() // Can safely be removed
export class Parent {
@Prop({ type: SchemaTypes.ObjectId, ref: 'Child' }) // Can safely be removed
child: PopulatedDoc<ChildDocument>;
}
Expected Behavior
I expect the code to run just fine. There are lot of reason to use bi-directional relation in order. I don't expect parent.child and child.parent to be populated at the same time anyway.
Looks like this issue was introduced in v6.4.1 with #11911. Fix will be in v6.5.1.