Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
nikzanda opened this issue Jun 12, 2024 · 1 comment
Closed
2 tasks done
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@nikzanda
Copy link

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.

@vkarpov15
Copy link
Collaborator

This is expected behavior. parentNode is not a property on Node model, so Mongoose doesn't do any casting on that property. So { parentNode: { $in: [parentNode._id] } } will work as expected, because parentNode._id is an ObjectId and Mongoose doesn't strip out unknown keys from query filters by default. But { parentNode: { $in: [parentNode.id] } } won't work, because Mongoose won't cast parentNode.id (type string) to ObjectId.

There are a couple of workarounds:

  1. Specify the discriminator key using the following syntax. If you explicitly specify you're looking for nodes of type ChildNode, Mongoose will be able to infer that parentNode should be an ObjectId:
  const nodeQueryResult1 = Node.find({
    kind: 'ChildNode',
    parentNode: {
      $in: [parentNode.id],
    },
  });
  1. Explicitly convert parentNode.id to an ObjectId:
  const nodeQueryResult1 = Node.find({
    parentNode: {
      $in: [new mongoose.Types.ObjectId(parentNode.id)],
    },
  });

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants