Alejandro A
Alejandro A

Reputation: 1190

Filter based on array in multiple documents

Using Python and I have the following data:

    [
        {
                "_id" : "owner-12567c18-9f6a-49d0-a0d7-4b5d4634a46f",
                "car-e2a17cdb-3c13-44ad-af66-d9cd31cdbf67" : [
                        15,
                        0,
                        0,
                        false
                ],
                "car-e2a27cdb-3c13-44ad-af66-d9cd31cdbf67" : [
                        32,
                        0,
                        0,
                        true
                ]
        },
        {
            "_id":'owner-xxx',
           'car_y': [
                x,
                x,
                x,
                false
            ]
         }
    ]

It follows the schema _id of an owner,then an entry for every car and a list containing 4 fields.

I would like to filter for every car of an owner the one that the last element is false, so only the car car-e2a17cdb-3c13-44ad-af66-d9cd31cdbf67 would be returned.

Upvotes: 0

Views: 37

Answers (1)

Kohelet
Kohelet

Reputation: 404

I think You do need qoutas to get JSON structure - is it the case of data provider?

After it You'll get dictionary structure:

d = [
    {
        "_id" : "owner-12567c18-9f6a-49d0-a0d7-4b5d4634a46f",
        "car-e2a17cdb-3c13-44ad-af66-d9cd31cdbf67" : ["15", "0", "0", "false"],
        "car-e2a27cdb-3c13-44ad-af66-d9cd31cdbf67" : ["32", "0", "0", "true"]
    }, 
    {
        "_id" : "owner-2",
        "car-e2a17cdb-3c13-44ad-af66-d9cd31cdbf67" : ["15", "0", "0", "false"],
        "car-e2a27cdb-3c13-44ad-af66-d9cd31cdbf67" : ["32", "0", "0", "true"],
        "car-special" : ["32", "0", "0", "false"]
    }
    ]

for owner in d:    
    for key in owner.keys():
        if (owner[key][3] == "false"):
            print('owner: ', owner['_id'], '| car: ', key)

#output:
#owner:  owner-12567c18-9f6a-49d0-a0d7-4b5d4634a46f | car:  car-e2a17cdb-3c13-44ad-af66-d9cd31cdbf67
#owner:  owner-2 | car:  car-e2a17cdb-3c13-44ad-af66-d9cd31cdbf67
#owner:  owner-2 | car:  car-special

Upvotes: 1

Related Questions