Description
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
Both projectQueryResult1
and projectQueryResult2
work as expected.
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.
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.