Skip to content

PopulatedDoc in find does not work correctly #15923

@hasezoey

Description

@hasezoey

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

9.0.2

Node.js version

25.2.1

MongoDB server version

unrelated

Typescript version (if applicable)

5.9.3

Description

When using PopulatedDoc with a Document that has DocType specified, all find's that are not of type Document dont work.
As can be seen in the extracted type below, refTo in the example seemingly becomes just Schema2, no more | ObjectId or anything else.

Note that when removing the explicit DocType from PopulatedDoc, the field becomes any in the find, likely because Document defaults to that.

Also note that when removing the custom interfaces, a new error comes at find({ refTo: doc2 }).

Steps to Reproduce

/* eslint-disable @typescript-eslint/no-unused-vars */
// NodeJS: 25.2.1
// Typescript 5.9.3
import * as mongoose from 'mongoose'; // [email protected]

interface Schema2 {
  test: string;
}
interface Schema1 {
  test: string;
  // remove "Schema2" from the "mongoose.Document" and observe it working
  // eslint-disable-next-line @typescript-eslint/no-empty-object-type
  refTo: mongoose.PopulatedDoc<mongoose.Document<mongoose.Types.ObjectId, {}, Schema2> & Schema2, mongoose.Types.ObjectId>;
}

const schema1 = new mongoose.Schema({
  test: String,
  refTo: {
    ref: 'Model2',
    type: mongoose.Types.ObjectId,
  },
});
const schema2 = new mongoose.Schema({
  test: String,
});
const model1 = mongoose.model<Schema1>('Model1', schema1);
const model2 = mongoose.model<Schema2>('Model2', schema2);

async function main() {
  await mongoose.connect(`mongodb://localhost:27017/`, {
    dbName: 'verifyMASTER',
  });

  const doc2 = await model2.create({ test: 'hello' });
  const doc1 = await model1.create({ test: 'ish', refTo: doc2._id });

  await model1.find({ refTo: doc2 });
  // Error: No overload matches this call.
  await model1.find({ refTo: doc2._id });
  // Error: No overload matches this call.
  await model1.find({ refTo: doc2._id.toString() });

  await mongoose.disconnect();
}

main();

Find Query type (at find({ refTo: doc2._id })):

(method) Model<Schema1, {}, {}, {}, Document<unknown, {}, Schema1, {}, DefaultSchemaOptions> & Schema1 & { _id: Types.ObjectId; } & { ...; }, any, Schema1>.find<mongoose.Document<unknown, {}, Schema1, {}, mongoose.DefaultSchemaOptions> & Schema1 & {
    _id: mongoose.Types.ObjectId;
} & {
    __v: number;
}>(filter: mongoose._QueryFilter<{
    test: string;
    refTo: Schema2;
    "refTo.test": never;
}>, projection: mongoose.ProjectionType<Schema1> | null | undefined, options: mongoose.QueryOptions<Schema1> & {
    lean: true;
} & mongoose.mongo.Abortable): mongoose.Query<(Schema1 & {
    _id: mongoose.Types.ObjectId;
} & {
    ...;
})[], ... 4 more ..., {}> (+3 overloads)

Reproduction Repository & Branch

Expected Behavior

To work like in mongoose 8.x.

Original issue is from typegoose typegoose/typegoose#1022.

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions