python - How to apply filter condition on two embedded field in django mongoengine? -


i using django mongoengine. tried query top solve this. tried raw query not success.

{   "_id" : objectid("556fe5c338a01311c4c4d1c1"),   "uuid" : "5c8ae1dfcb1d060d5a951d96d4798a84cdf090e9",   "snapshot_values" : [{       "key" : "gender",       "value" : "female",     }, {       "key" : "marital_status",       "value" : "married",     }], },  {   "_id" : objectid("556fe5c338a01311c4c4d1c1"),   "uuid" : "5c8ae1dfcb1d060d5a951d96d4798a84cdf090e9",   "snapshot_values" : [{       "key" : "gender",       "value" : "female",     }, {       "key" : "marital_status",       "value" : "unmarried",     }], },  {   "_id" : objectid("556fe5c338a01311c4c4d1c1"),   "uuid" : "5c8ae1dfcb1d060d5a951d96d4798a84cdf090e9",   "snapshot_values" : [{       "key" : "gender",       "value" : "female",     }, {       "key" : "marital_status",       "value" : "married",     }], }, 

here want apply select data has key=marital_status , value=married.

condition1: {key=marital_status, value=married},   return 2 data,  condition:2 {key=marital_status,value=unmarried}, , {key=gender,value=female}, return singe raw data above data. 

anyone know how make query satisfy above conditions.

pleas give me suggestion, little help, suggestion helpful me.

thanks in advance.

for first condition, try following query uses dot notation access elements of array , access fields of embedded document:

db.user.find({     "snapshot_values.key": "marital_status",      "snapshot_values.value": "married" }) 

for second condition, use following query takes advantage of $in operator:

db.user.find({     "snapshot_values.key": { "$in": ["marital_status", "gender"] },      "snapshot_values.value": { "$in": ["unmarried", "female"] } }) 

-- update --

the django mongoengine version should following example can use q class:

for first condition

person_snapshot.objects(q(snapshot_values__key="marital_status") & q(snapshot_values__value="marries") 

second condition (uses query operator in)

key_list = ["marital_status", "gender"] value_list = ["unmarried", "female"] person_snapshot.objects(q(snapshot_values__key__in=key_list) & q(snapshot_values__value__in=value_list) 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

spring cloud - How to configure SpringCloud Eureka instance to point to https on non standard port -

javascript - Bootstrap Popover: iOS Safari strange behaviour -