Skip to content

Query recursive model with discriminator not working with string ref id filter #14664

Closed
@nikzanda

Description

@nikzanda

Prerequisites

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

Mongoose version

8.4.1

Node.js version

20.10.0

MongoDB server version

6.0.2

Typescript version (if applicable)

5.4.5

Description

Reproduction link: https://stackblitz.com/edit/stackblitz-starters-ypt8r8?file=src%2Findex.ts

I have a model named “Node” with two discriminators, “ParentNode” and “ChildNode” respectively. The ChildNode subclass has a “parentNode” property of type ObjectId, which refers to the “ParentNode” subclass. I noticed that when querying the Node model by the “parentNode” field using a string id, the query does not work. However, it works when using the ObjectId. In MongoDB, this use case is correct. But in Mongoose, when querying a model by an ObjectId property using only a string value, the query works just as well. Therefore, I expect the same behavior for subModels.

The nodeQueryResult1 does not work, while nodeQueryResult2 does
image

Both projectQueryResult1 and projectQueryResult2 work as expected.
image

Please note that the nodeQueryResult1 query does not work when passing an id of string type, while the projectQueryResult query works with both a string type id and an ObjectId type id.
Screenshot 2024-06-12 at 11 45 47

Steps to Reproduce

Clone the repository locally, compile the TypeScript code and run the command:

npx tsc && node --experimental-specifier-resolution=node dist/index.js`

Expected Behavior

I expect that if I can query any model by a reference property of type ObjectId using a string value, I should also be able to query a subclass in the same manner.

Metadata

Metadata

Assignees

No one assigned

    Labels

    helpThis issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions