-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Mongoose Discriminator - ObjectId / Array type #10130
Comments
Similarly, today, I was attempting to get this to work the other way around.
This can then be consumed with a dynamic model, created at run-time:
In essence, I'm attempting to utilise the tenantRefs array to automatically filter depending on the user's tenantRef. If you can imagine a marketplace scenario, there's an array including a supplier ID and a client ID. This works perfectly on all query/aggregation methods, but frustratingly, not when creating a document. Due to this snippet in /lib/document.js, the array is split out:
This: Temporary work around |
The tricky part is checking whether discriminator values are equal, so it is unlikely we'll ever allow a pojo as a discriminator tied value for example. But we should be able to easily support ObjectIds and numbers as tied values. Is there some other type you need? |
Great that you've added ObjectIds to the scope @vkarpov15 - helps to reduce duplicated fields in MongoDb, thank you! 🙌 An array of values (string/objectId/number) would be ideal if no pojo, but imagine that's a fair amount of duplicated code on your end, and it would be unclear if it's "all" or "in" for the comparison without additional configuration options. |
Array of values will be tricky, because comparing arrays in JavaScript is complex in general. What's your use case for using an array as a tiedValue? |
Apologies for the delay. I utilise discriminators for schema-based tenant isolation. Cross-functional team example To find the teams that a user has, the userId could be within a "users" array field on the Team collection - this works fine. This can be gotten around with a users field on the project collection too, however it could grow significantly over time and requires a separate logic condition to update projects when adding users to teams. I'd say it's "nice to have" an an option. I'm considering writing a separate plugin to act as middleware on all methods in the future; I think using discriminators is good, but not quite what it was built for. The enhancement of being able to use Numbers and ObjectIds is a fantastic result anyway, so thank you. FYI - I'll copy my second comment into a new issue, as it appears to have been missed. |
Do you want to request a feature or report a bug?
Bug that's possibly a feature.
What is the current behavior?
A discriminator value is omitted when not a string.
Why is this a problem?
This is frustrating, as I'm unable to use any query operations such as $in, or an existing ObjectId.
I'm unsure why this is tied to only be a string, as it actually works well with either of the non-string values such as:
{$in: array}
or
ObjectId('xyz')
What is the expected behavior?
To be able to use non-string discriminators.
What are the versions of Node.js, Mongoose and MongoDB you are using?
Node v14.16.0
Mongoose v5.12.1
MongoDb v4.4.4
Code specific comment
I have identified the specific line of code which is causing the issue:
mongoose/lib/helpers/model/discriminator line 65.
This can be updated to (or the logic omitted entirely):
Other comments
Only thought is there's some sort of security concern?
The text was updated successfully, but these errors were encountered: