Hi,
I have noticed that the object ID is stored as a string in MongoDB. Is there a way to store it as ObjectID? It would be easier if the type is ObjectID for easier lookups.
I am referring to the tutorial here:
Anyone?
Hey
@442045958042288128— we store object IDs as strings intentionally, and there is no real easy way to store them as full ObjectIDs. This is for a few reasons, but we could certainly evaluate a way to be able to "opt in" to ObjectIDs instead of string representations in a new feature
there are a few places in the code that would need to be adjusted, but technically, this could be doable with a PR
right now, it's not possible though
Is there a way to handle this with custom code? Example, by using hooks?
probably yes
maybe a
beforeChange
collection hook and a
beforeRead
collection hook
the
beforeChange
to transform any relationship field string IDs to ObjectIDs before storage, and the
beforeRead
to convert them back to strings
Hey
@1111488483672989696— I just looked into this and built a plugin to do exactly what you thought - - just add a field hook to each relationship / upload field to convert to ObjectIDs before storage:
nice and simple 👍 good question! this is a nice thing to have!
Thanks will check it...
This solution is working. Thanks for the quick fix. Still just wanted to know if this is under testing or not? Because I did not see the details updated in the payload documentation of the relationship. Could you confirm this is an official release or not?
This is an official release, but it is a plugin, so it won't be listed in the Payload docs directly as it's not a part of "core" specifically. But it is officially supported and will continue to be
in the future, we will be listing all of our official plugins in the Payload docs, and this one will be listed as well
Hey
@364124941832159242I have noticed an issue, the filter is not working as expected for relationship field. Could you confirm that, the plugin you implemented works or not for relationship fields filtering?
let me check on this immediately
It appears relationship filtering is not working as expected. Looking into a fix now.
Issue logged here. Expect this to be taken care of in a day or two.
hey
@1111488483672989696- just a heads-up, we fixed this!
you shouldn't need to update your plugin, instead, you should just update your Payload version to the newest
i also added a test suite to the plugin to make sure that it's nice and covered
Star
Discord
online
Get dedicated engineering support directly from the Payload team.