python - Mongoengine: Query a MapField -
i have map field want query by. like:
class user(mongoengine.document): email = mongoengine.emailfield(required=false, unique=false) username = mongoengine.stringfield(max_length=30, min_length=6, required=true, unique=true) password = mongoengine.stringfield(max_length=500, min_length=6, required=true) profiles = mongoengine.mapfield(mongoengine.embeddeddocumentfield(deviceprofile))
so in field, profiles, store objects so: device_id: deviceprofile object.
i tried: user.objects(profiles__device_id=device_id)
no avail. how query return user objects have specific key in profiles field? basically, want query user documents contain deviceprofile object based on device id.
leaving here else runs problem.
in order retrieve mongoengine document key of map field, use exists
operator. example, query can constructed , passed object method:
qry = { 'profiles__{}__exists'.format(key): true, '_cls': 'user' } user.object(**qry)
treating exists
operator "regular" query doesn't work since non-null, non-zero value treated true
, match return documents there in mapfield. example:
users.object(profiles__exists=key)
will return objects has non-empty mapfield.
Comments
Post a Comment