python - Conditions in Find, Mongo -


i have mongo collection doc follows:-

{     "_id" : objectid("55a9378ee2874f0ed7b7cb7e"),     "_uid" : 10,     "impressions" : [             {                     "pos" : 6,                     "id" : 123,                     "service" : "furniture"             },             {                     "pos" : 0,                     "id" : 128,                     "service" : "electronics"             },             {                     "pos" : 2,                     "id" : 127,                     "service" : "furniture"             },             {                     "pos" : 2,                     "id" : 125,                     "service" : "electronics"             },             {                     "pos" : 10,                     "id" : 124,                     "service" : "electronics"             }     ] }, {     "_id" : objectid("55a9378ee2874f0ed7b7cb7f"),     "_uid" : 11,     "impressions" : [             {                     "pos" : 1,                     "id" : 124,                     "service" : "furniture"             },             {                     "pos" : 10,                     "id" : 124,                     "service" : "electronics"             },             {                     "pos" : 1,                     "id" : 123,                     "service" : "furniture"             },             {                     "pos" : 21,                     "id" : 122,                     "service" : "furniture"             },             {                     "pos" : 3,                     "id" : 125,                     "service" : "electronics"             },             {                     "pos" : 10,                     "id" : 121,                     "service" : "electronics"             }     ] } 

my aim find "id" in particular "service" "furniture" i.e results this:

[122,123,124,127] 

but i'm not able figure out how frame condition in

db.collection_name.find() 

because of difficulty of having condition 'n' th element in array, "impressions[n]":"value".

one option use "id"s obtained perform aggregate operation find impressions each "id" service suggested answer question asked earlier:- mapreduce in pymongo.

but want list of distinct 'id' in service not impressions. kindly help!

you need aggregration framework meaningful results. this:

result = db.collection.aggregate([     { "$match": {         "impressions.service": "furniture"     }},     { "$unwind": "$impressions" },     { "$match": {         "impressions.service": "furniture"     }},     { "$group": {         "_id": "$impressions.id"     }} ]) 

or better yet mongodb 2.6 or greater, can remove array items unmatched "prior" $unwind $redact:

result = db.collection.aggregate([     { "$match": {         "impressions.service": "furniture"     }},     { "$redact": {        "$cond": {            "if": {                 "$eq": [                    { "$ifnull": [ "$service", "furniture" ] },                    "furniture"                ]            },            "then": "$$descend",            "else": "$$prune"        }     }},     { "$unwind": "$impressions" },     { "$group": {         "_id": "$impressions.id"     }} ]) 

which yields:

{ "_id" : 122 } { "_id" : 124 } { "_id" : 127 } { "_id" : 123 } 

not plain "list", transform it, therefore :

def mapper (x):     return x["_id"]  map(mapper,result) 

or:

map(lambda x: x["_id"], result) 

to give you:

[122, 124, 127, 123] 

if want "sorted" either add $sort stage @ end of aggregation pipeline or sort resulting list in code.


Comments