mongoengine
mongoengine copied to clipboard
Cannot attempt to query an object id field with a value that is not an object id.
https://github.com/MongoEngine/mongoengine/blob/51afeca747838614d2545460d83e43ac0c313d4b/mongoengine/base/fields.py#L519
So I was writing some code to allow the client of my API to search for a resource by specifying either the built-in mongodb _id
field or a uuid
field that I had added to my Document. My code looked like the following.
def get(self, id):
q = Q(_id=id) | Q(uuid=id)
Resource.objects.get(q)
However when I ran this I got the following error.
raise ValidationError(message, errors=errors, field_name=field_name) mongoengine.errors.ValidationError: '{my-uuid}' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string
It looks like in prepare_query_value()
for the ObjectIdField
class, it calls to_mongo()
which raises an exception if the value is NOT an object id.
I feel like this is not a great design as the developer should be able to at least query for a non object id value even if the field technically should be.