mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

is it possible to search null objects ?

Open prabhupad26 opened this issue 3 years ago • 7 comments

I'm trying to search null object with python NoneType but got the below error :

raise ValidationError(message, errors=errors, field_name=field_name) mongoengine.errors.ValidationError: 'null' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string

prabhupad26 avatar Aug 04 '22 13:08 prabhupad26

Code for reproducing this issue:

class User(mongoengine.Document):
meta = {
    "db_alias": 'core',
    "collection": 'so_users_details'
}
user_name = mongoengine.StringField(required=True)
tag_details = mongoengine.ObjectIdField(null=True)

User(user_name='Sam').save() 
User.objects().filter(tag_details=None)

prabhupad26 avatar Aug 04 '22 14:08 prabhupad26

does it make sense to add a null check here in method to_mongo in class ObjectIdField ?

prabhupad26 avatar Aug 04 '22 14:08 prabhupad26

@prabhupad26 I just posted a similar issue here. https://github.com/MongoEngine/mongoengine/issues/2684

I think the real issue is that prepare_query_value() on the ObjectIdField class has been overridden to call to_mongo() when it shouldn't have been overridden in the first place.

Basically you cannot search for any value other than an ObjectId value on an ObjectIdField which is super limiting.

rykroon-momentum avatar Aug 09 '22 19:08 rykroon-momentum

@rykroon-momentum
I think that has been done to ensure that ObjectId field should not have any other type of value. But there should be some handling for null objects as well.

prabhupad26 avatar Aug 09 '22 21:08 prabhupad26

@prabhupad26 So prepare_query_value() has nothing to do with the actual storing of the value, so if this method were removed it would not interfere with storing values that are not of type ObjectId. BUT! it would allow users to search for values that are not of type ObjectId which should be allowed.

rykroon-momentum avatar Aug 10 '22 13:08 rykroon-momentum

@rykroon-momentum so what are you suggesting ? and adding a null check here won't do any good in this case ?

prabhupad26 avatar Aug 13 '22 12:08 prabhupad26

@prabhupad26 So the change you are suggesting would help your specific issue, but it wouldn't resolve the underlying issue. For example if you look at my ticket for the situation that I was experiencing https://github.com/MongoEngine/mongoengine/issues/2684, it would not be resolved by your solution. What I am suggesting is to remove the to_mongo() method from the ObjectIdField, therefore it would inherit the to_mongo() method from the parent class which is BaseField.

rykroon-momentum avatar Aug 15 '22 12:08 rykroon-momentum