Skip to content

Conversation

@vkarpov15
Copy link
Collaborator

@vkarpov15 vkarpov15 commented Dec 14, 2025

Summary

Fix #15779
Fix #15887

MongoDB Node driver's RootFilterOperators uses Filter, which looks like this:

export declare type Filter<TSchema> = {
    [P in keyof WithId<TSchema>]?: Condition<WithId<TSchema>[P]>;
} & RootFilterOperators<WithId<TSchema>>;

WithId adds a default _id?: ObjectId, even for Record<string, any>. Without the change in this PR, that _id wouldn't get any Mongoose casting applied, which is what causes the issue in #15779 (comment)

Examples

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a TypeScript type inference issue where MongoDB's implicitly-added _id field wasn't receiving proper Mongoose casting in query filters. The change ensures that when using QueryFilter with types that don't explicitly define _id, the MongoDB driver's default _id?: ObjectId still gets Mongoose's query type casting applied (allowing both string and Types.ObjectId values).

Key Changes:

  • Modified _QueryFilter type to use mongodb.WithId<T> for the RootFilterOperators parameter
  • Added test case demonstrating the fix with aggregate queries using QueryFilter

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 3 comments.

File Description
types/query.d.ts Updated _QueryFilter to apply WithId<T> to the schema passed to RootFilterOperators, ensuring MongoDB's default _id field receives Mongoose casting
test/types/queries.test.ts Added gh15779_2 test case that verifies QueryFilter<Job> compiles correctly when used with aggregate's $match stage

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Dec 15, 2025
@vkarpov15 vkarpov15 merged commit bc5873b into master Dec 15, 2025
5 checks passed
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-15779-2 branch December 15, 2025 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

typescript Types or Types-test related issue / Pull Request

Projects

None yet

3 participants