How to set the value of a $match query based on $cond for aggregation in Mongodb

147 Views Asked by At

I am trying to set the value of one of my query fields based on the value from a previous field. So first I run a match to find all file names of the files within a given campaign. I then use the filenames from these initial matches to look for all entries with a matching file name but from a different 'chain' (denoted by 'id_chain').

The problem I am having is that we have 3 different sensors that this aggregation should work for, but one of the 3 does not have a field called 'bands'. As such, I want to determine the value of the 'bands' field based on which sensor name is present in the initial file matching. I have tried to use a $cond as such:

"$expr": {
        "$cond":{'if': {"$$sens": {"$in": ["Sony1", "Sony2"]}},
                 'then': {"band": {"$in": [0,1,2}},
                 'else':{'band': {"$exists": False}
                 }
                                                     
          }

Basically what I want is that if the sensor is either Sony1 or Sony2 then I want it to set:

"band": {"$in": [0,1,2]}

and if the sensor is not one of these two Sonys then:

"band": {"$exists": False}

But this doesn't seem to be working as the results come up empty each time.

Below is the full code I have been trying:

q_file={
        "id_sensor": "Sony1",
        "id_campaign": "5424",
        "id_chain" : {"$exists": False}
          }


files = db.collections["files"].aggregate([
            # Search for files defined by q_file
            {"$match": q_file},
            # Search for files with matching names
            {"$lookup": {
                "from": "files",
                "let": {"name": "$file_name",
                        "camp": "$id_campaign",
                        "sens": "$id_sensor"},
                "pipeline": [{"$match": {"id_chain" : "ConvertedFiles",
                                         
                                         "id_sensor": "Sony1",
                                         "$expr": {
                                             "$cond":{'if': {"$$sens": {"$in": ["Sony1", 
                                                                               "Sony2"]}},
                                                      'then': {"band": {"$in": [0,1,2]}},
                                                      'else':{'band': {"$exists": False}}
                                                     
                                                    }
                                                    
                                         },

                                         "$expr": {"$and":[
                                                         {"$eq": ["$file_name", "$$name"]},
                                                         {"$eq": ["$id_campaign", "$$camp"]}
                                                     ]
                                                  }
                }}],
                "as": "all_bands"
            }}

And also an example of the documents I have in the database:

{'file_name': '257', 'band': 0, 'id': '/home/data/Sony1/FileConvert/257_0.tif', 'id_sensor': 'Sony1', 'id_campaign': '5424', 'id_chain': 'ConvertedFiles'}}

{'file_name': '257', 'band': 1, 'id': '/home/data/Sony1/FileConvert/257_1.tif', 'id_sensor': 'Sony1', 'id_campaign': '5424', 'id_chain': 'ConvertedFiles'}}

{'file_name': '257', 'band': 2, 'id': '/home/data/Sony1/FileConvert/257_2.tif', 'id_sensor': 'Sony1', 'id_campaign': '5424', 'id_chain': 'ConvertedFiles'}}

{'file_name': '257', 'id': '/home/data/Thermal/FileConvert/257.tif', 'id_sensor': 'Thermal', 'id_campaign': '5424', 'id_chain': 'ConvertedFiles'}}

{'file_name': '257', 'id': '/home/data/Sony1/raw/257.tif', 'id_sensor': 'Sony1', 'id_campaign': '5424'}}

{'file_name': '257', 'id': '/home/data/Thermal/raw/257_3.tif', 'id_sensor': 'Thermal', 'id_campaign': '5424'}}
0

There are 0 best solutions below