Determining time overlapping between two different kinds of files from the same collection (II)

Hi there:

Some weeks ago I was trying to get a solution to this problem:

I got it, but the data format of the files has changed a little bit:

Broadcasting files:

{
  "idSensor": 3,
  "idDevice": 55,
  "dateTime": 
  {
    "instant": "2020-07-24T10:00:40Z",
    "offset": "+02:00"
  },
  "data": 
  {
    "inicio": "2020-07-24T12:00:40+02:00",
    "fin": "2020-07-24T12:00:52+02:00",
    "archivo": " EKT_captaREVOLUTION_VERT_1_corto.mp4",
    "tipo": "video"
  }
}

Reaction files:

{
  "idSensor": 2,
  "idDevice": 48,
  "dateTime": 
  {
    "instant": "2020-06-09T14:01:58.521Z",
    "offset": "+02:00"
  },
  "data": 
  {
    "BeginTime": "2020-06-09T16:01:58.521891+02:00",
    "CountingLines": 
    [{
      "Direction": "EnteredLeaving",
      "LineID": 0
    }],
    "FaceInfo": 
    {
      "Age": 32.03,
      "Emotion": "SURPRISE",
      "IsDetected": true,
      "MaleProbability": 0.91,
      "gazeTime": 0.36,
      "numGazes": 4
    },
    "ImageSize": 
    {
      "Height": 1080,
      "Width": 1920
    },
    "LookingDuration": 1,
    "PersonID": 102340,
    "ReIDInfo": {"NumReIDs": 1},
    "RoiInfo": {"RoiDuration": 10.909090909090908},
    "SensorID": 51,
    "SocialDistance": [],
    "TrackingDuration": 11.64,
    "Trajectory": null,
    "direction": null,
    "id": 1,
    "roiName": 1,
    "roiType": 1
  }
}

So my query:

var pipeline = 
[
	{
		"$match": //Deja pasar sólo los archivos de emisiones y de reacciones
		{
		    "$or": [{"idSensor": 2}, {"idSensor": 3}]
		}
	},
    {
        "$lookup": //Une cada documento con los demás -incluyendo consigo mismo-, que agrupa en un array con tantas posiciones como documentos
        {
            "from": "sensorsData",
            "localField": "idDevice",
            "foreignField": "idDevice",
            "as": "array"
        }
    },
    {
        "$unwind": "$array" //Descompone los archivos en función de las posiciones del array
    },
    {
        "$match": //Deja pasar sólo aquellos archivos con estructura emisión-reacción y elimina las demás combinaciones: emisión-emisión, reacción-reacción y reacción-emisión (redundantes)
        {
            "$and": 
            [
                {"data.inicio": {"$exists": true}}, 
                {"array.data.BeginTime": {"$exists": true}}
            ]
        }
    },
    {
        "$addFields": //Creación de los parámetros temporales
        {
            "dtBroadcastStart": {"$toDate": "$data.inicio"},
            "dtBroadcastEnd": {"$toDate": "$data.fin"},
            "dtTrackingStart": {"$toDate": "$array.data.BeginTime"},
			"dtTrackingEnd": {"$add": [{"$toDate": "$array.data.BeginTime"}, {"$multiply": ["$array.data.TrackingDuration", 1000]}]}
        }
    },
    {
        "$match": //Filtrado de los documentos que cumplen las condiciones de solapamiento temporal
        {
            "$expr":
            {
                "$and": 
                [
                    {"$lt": ["$dtBroadcastStart", "$dtTrackingEnd"]},
                    {"$gt": ["$dtBroadcastEnd", "$dtTrackingStart"]}
                ]
            }
        }
    },
    {
        "$project": //Selección final de parámetros
        {
            "_id": 0,
            "idDevice": "$idDevice",
            "naBroadcast": "$data.archivo",
			"naType": "$data.tipo",
            "dtBroadcastStart": 1,
            "dtBroadcastEnd": 1,
            "qtBroadcastDurationS": {"$divide": [{"$subtract": [{"$toDate": "$dtBroadcastEnd"}, {"$toDate": "$dtBroadcastStart"}]}, 1000]}
            "naWeekday": 
			{
			    "$switch": 
			    {
			        "branches": 
			        [
			            {"case": {"$eq": [{"$dayOfWeek": {"$toDate": "$data.inicio"}}, 1]}, "then": "Domingo"},
			            {"case": {"$eq": [{"$dayOfWeek": {"$toDate": "$data.inicio"}}, 2]}, "then": "Lunes"},
			            {"case": {"$eq": [{"$dayOfWeek": {"$toDate": "$data.inicio"}}, 3]}, "then": "Martes"},
			            {"case": {"$eq": [{"$dayOfWeek": {"$toDate": "$data.inicio"}}, 4]}, "then": "Miércoles"},
			            {"case": {"$eq": [{"$dayOfWeek": {"$toDate": "$data.inicio"}}, 5]}, "then": "Jueves"},
			            {"case": {"$eq": [{"$dayOfWeek": {"$toDate": "$data.inicio"}}, 6]}, "then": "Viernes"},
			            {"case": {"$eq": [{"$dayOfWeek": {"$toDate": "$data.inicio"}}, 7]}, "then": "Sábado"}
			        ],
			        "default": "Fecha incorrecta"
			    }
			},
			"naMonth":
			{
			    "$switch": 
			    {
			        "branches": 
			        [
			            {"case": {"$eq": [{"$substr": ["$data.inicio", 5, 2]}, "01"]}, "then": "Enero"},
			            {"case": {"$eq": [{"$substr": ["$data.inicio", 5, 2]}, "02"]}, "then": "Febrero"},
			            {"case": {"$eq": [{"$substr": ["$data.inicio", 5, 2]}, "03"]}, "then": "Marzo"},
			            {"case": {"$eq": [{"$substr": ["$data.inicio", 5, 2]}, "04"]}, "then": "Abril"},
			            {"case": {"$eq": [{"$substr": ["$data.inicio", 5, 2]}, "05"]}, "then": "Mayo"},
			            {"case": {"$eq": [{"$substr": ["$data.inicio", 5, 2]}, "06"]}, "then": "Junio"},
			            {"case": {"$eq": [{"$substr": ["$data.inicio", 5, 2]}, "07"]}, "then": "Julio"},
			            {"case": {"$eq": [{"$substr": ["$data.inicio", 5, 2]}, "08"]}, "then": "Agosto"},
			            {"case": {"$eq": [{"$substr": ["$data.inicio", 5, 2]}, "09"]}, "then": "Septiembre"},
			            {"case": {"$eq": [{"$substr": ["$data.inicio", 5, 2]}, "10"]}, "then": "Octubre"},
			            {"case": {"$eq": [{"$substr": ["$data.inicio", 5, 2]}, "11"]}, "then": "Noviembre"},
			            {"case": {"$eq": [{"$substr": ["$data.inicio", 5, 2]}, "12"]}, "then": "Diciembre"}
			        ],
			        "default": "Fecha incorrecta"
			    }
			},
            "idPerson": "$array.data.PersonID",
			"dtTrackingStart": 1,
			"dtTrackingEnd": 1,
			"qtFaceDetected": 
			{
				"$cond": 
				{
					"if": {"$eq": ["$array.data.FaceInfo.IsDetected", true]}, 
					"then": 1, 
					"else": 0
				}
			},
			"qtMaleProbability": "$array.data.FaceInfo.MaleProbability",
			"qtAge": "$array.data.FaceInfo.Age",
			"naEmotion": "$array.data.FaceInfo.Emotion",
			"qtGaze": "$array.data.FaceInfo.numGazes",
			"qtGazeDurationS": "$array.data.FaceInfo.gazeTime",
			"qtFaceDurationS": "$array.data.LookingDuration",
			"qtTrackingDurationS": "$array.data.TrackingDuration",
			"qtReId": "$array.data.ReIDInfo.NumReIDs"
        }
	}
]

db.sensorsData.aggregate(pipeline)

It works, but it seems this stage:

{
    "$match": //Filtrado de los documentos que cumplen las condiciones de solapamiento temporal
    {
        "$expr":
        {
            "$and": 
            [
                {"$lt": ["$dtBroadcastStart", "$dtTrackingEnd"]},
                {"$gt": ["$dtBroadcastEnd", "$dtTrackingStart"]}
            ]
        }
    }
},

It’s too much when querying against the production DB, and it gets stuck. Any idea about how to optimize my query?

Thanks in advance!

Hi @Javier_Blanco,

Why your query lookup from sensorData to sensorData ? Is all data in the same collection?

If you need your query execution analysis please provide:

 db.sensorsData.explain().aggregate(pipeline);
db.sensorData.getIndexes();
db.sensorData.stats();

Thanks,
Pavel

Hi, Pavel; thanks for your answer.

Yes, both kind of files are within the same collection.

db.sensorsData.explain().aggregate(pipeline);

{
    	"stages" : [
    		{
    			"$cursor" : {
    				"query" : {
    					"$and" : [
    						{
    							"$or" : [
    								{
    									"idSensor" : 2
    								},
    								{
    									"idSensor" : 3
    								}
    							]
    						},
    						{
    							"data.inicio" : {
    								"$exists" : true
    							}
    						}
    					]
    				},
    				"fields" : {
    					"array.data.BeginTime" : 1,
    					"array.data.FaceInfo.Age" : 1,
    					"array.data.FaceInfo.Emotion" : 1,
    					"array.data.FaceInfo.IsDetected" : 1,
    					"array.data.FaceInfo.MaleProbability" : 1,
    					"array.data.FaceInfo.gazeTime" : 1,
    					"array.data.FaceInfo.numGazes" : 1,
    					"array.data.LookingDuration" : 1,
    					"array.data.PersonID" : 1,
    					"array.data.ReIDInfo.NumReIDs" : 1,
    					"array.data.TrackingDuration" : 1,
    					"data.archivo" : 1,
    					"data.fin" : 1,
    					"data.inicio" : 1,
    					"data.tipo" : 1,
    					"dtBroadcastEnd" : 1,
    					"dtBroadcastStart" : 1,
    					"dtTrackingEnd" : 1,
    					"dtTrackingStart" : 1,
    					"idDevice" : 1,
    					"_id" : 0
    				},
    				"queryPlanner" : {
    					"plannerVersion" : 1,
    					"namespace" : "mercury.sensorsData",
    					"indexFilterSet" : false,
    					"parsedQuery" : {
    						"$and" : [
    							{
    								"$or" : [
    									{
    										"idSensor" : {
    											"$eq" : 2
    										}
    									},
    									{
    										"idSensor" : {
    											"$eq" : 3
    										}
    									}
    								]
    							},
    							{
    								"data.inicio" : {
    									"$exists" : true
    								}
    							}
    						]
    					},
    					"queryHash" : "7B4F946C",
    					"planCacheKey" : "1E9CCC0D",
    					"winningPlan" : {
    						"stage" : "FETCH",
    						"filter" : {
    							"data.inicio" : {
    								"$exists" : true
    							}
    						},
    						"inputStage" : {
    							"stage" : "OR",
    							"inputStages" : [
    								{
    									"stage" : "IXSCAN",
    									"keyPattern" : {
    										"idSensor" : 1,
    										"idDevice" : 1,
    										"dateTime" : 1
    									},
    									"indexName" : "idSensor_1_idDevice_1_dateTime_1",
    									"isMultiKey" : false,
    									"multiKeyPaths" : {
    										"idSensor" : [ ],
    										"idDevice" : [ ],
    										"dateTime" : [ ]
    									},
    									"isUnique" : false,
    									"isSparse" : false,
    									"isPartial" : false,
    									"indexVersion" : 2,
    									"direction" : "forward",
    									"indexBounds" : {
    										"idSensor" : [
    											"[3.0, 3.0]"
    										],
    										"idDevice" : [
    											"[MinKey, MaxKey]"
    										],
    										"dateTime" : [
    											"[MinKey, MaxKey]"
    										]
    									}
    								},
    								{
    									"stage" : "IXSCAN",
    									"keyPattern" : {
    										"idSensor" : 1
    									},
    									"indexName" : "idSensor_1",
    									"isMultiKey" : false,
    									"multiKeyPaths" : {
    										"idSensor" : [ ]
    									},
    									"isUnique" : false,
    									"isSparse" : false,
    									"isPartial" : false,
    									"indexVersion" : 2,
    									"direction" : "forward",
    									"indexBounds" : {
    										"idSensor" : [
    											"[2.0, 2.0]"
    										]
    									}
    								}
    							]
    						}
    					},
    					"rejectedPlans" : [
    						{
    							"stage" : "FETCH",
    							"filter" : {
    								"data.inicio" : {
    									"$exists" : true
    								}
    							},
    							"inputStage" : {
    								"stage" : "IXSCAN",
    								"keyPattern" : {
    									"idSensor" : 1,
    									"idDevice" : 1,
    									"dateTime" : 1
    								},
    								"indexName" : "idSensor_1_idDevice_1_dateTime_1",
    								"isMultiKey" : false,
    								"multiKeyPaths" : {
    									"idSensor" : [ ],
    									"idDevice" : [ ],
    									"dateTime" : [ ]
    								},
    								"isUnique" : false,
    								"isSparse" : false,
    								"isPartial" : false,
    								"indexVersion" : 2,
    								"direction" : "forward",
    								"indexBounds" : {
    									"idSensor" : [
    										"[2.0, 2.0]",
    										"[3.0, 3.0]"
    									],
    									"idDevice" : [
    										"[MinKey, MaxKey]"
    									],
    									"dateTime" : [
    										"[MinKey, MaxKey]"
    									]
    								}
    							}
    						},
    						{
    							"stage" : "FETCH",
    							"filter" : {
    								"data.inicio" : {
    									"$exists" : true
    								}
    							},
    							"inputStage" : {
    								"stage" : "OR",
    								"inputStages" : [
    									{
    										"stage" : "IXSCAN",
    										"keyPattern" : {
    											"idSensor" : 1,
    											"idDevice" : 1,
    											"dateTime" : 1
    										},
    										"indexName" : "idSensor_1_idDevice_1_dateTime_1",
    										"isMultiKey" : false,
    										"multiKeyPaths" : {
    											"idSensor" : [ ],
    											"idDevice" : [ ],
    											"dateTime" : [ ]
    										},
    										"isUnique" : false,
    										"isSparse" : false,
    										"isPartial" : false,
    										"indexVersion" : 2,
    										"direction" : "forward",
    										"indexBounds" : {
    											"idSensor" : [
    												"[2.0, 2.0]"
    											],
    											"idDevice" : [
    												"[MinKey, MaxKey]"
    											],
    											"dateTime" : [
    												"[MinKey, MaxKey]"
    											]
    										}
    									},
    									{
    										"stage" : "IXSCAN",
    										"keyPattern" : {
    											"idSensor" : 1
    										},
    										"indexName" : "idSensor_1",
    										"isMultiKey" : false,
    										"multiKeyPaths" : {
    											"idSensor" : [ ]
    										},
    										"isUnique" : false,
    										"isSparse" : false,
    										"isPartial" : false,
    										"indexVersion" : 2,
    										"direction" : "forward",
    										"indexBounds" : {
    											"idSensor" : [
    												"[3.0, 3.0]"
    											]
    										}
    									}
    								]
    							}
    						},
    						{
    							"stage" : "FETCH",
    							"filter" : {
    								"data.inicio" : {
    									"$exists" : true
    								}
    							},
    							"inputStage" : {
    								"stage" : "IXSCAN",
    								"keyPattern" : {
    									"idSensor" : 1
    								},
    								"indexName" : "idSensor_1",
    								"isMultiKey" : false,
    								"multiKeyPaths" : {
    									"idSensor" : [ ]
    								},
    								"isUnique" : false,
    								"isSparse" : false,
    								"isPartial" : false,
    								"indexVersion" : 2,
    								"direction" : "forward",
    								"indexBounds" : {
    									"idSensor" : [
    										"[2.0, 2.0]",
    										"[3.0, 3.0]"
    									]
    								}
    							}
    						}
    					]
    				}
    			}
    		},
    		{
    			"$lookup" : {
    				"from" : "sensorsData",
    				"as" : "array",
    				"localField" : "idDevice",
    				"foreignField" : "idDevice",
    				"unwinding" : {
    					"preserveNullAndEmptyArrays" : false
    				},
    				"matching" : {
    					"data.BeginTime" : {
    						"$exists" : true
    					}
    				}
    			}
    		},
    		{
    			"$addFields" : {
    				"dtBroadcastStart" : {
    					"$convert" : {
    						"input" : "$data.inicio",
    						"to" : {
    							"$const" : "date"
    						}
    					}
    				},
    				"dtBroadcastEnd" : {
    					"$convert" : {
    						"input" : "$data.fin",
    						"to" : {
    							"$const" : "date"
    						}
    					}
    				},
    				"dtTrackingStart" : {
    					"$convert" : {
    						"input" : "$array.data.BeginTime",
    						"to" : {
    							"$const" : "date"
    						}
    					}
    				},
    				"dtTrackingEnd" : {
    					"$add" : [
    						{
    							"$convert" : {
    								"input" : "$array.data.BeginTime",
    								"to" : {
    									"$const" : "date"
    								}
    							}
    						},
    						{
    							"$multiply" : [
    								"$array.data.TrackingDuration",
    								{
    									"$const" : 1000
    								}
    							]
    						}
    					]
    				}
    			}
    		},
    		{
    			"$match" : {
    				"$expr" : {
    					"$and" : [
    						{
    							"$lt" : [
    								"$dtBroadcastStart",
    								"$dtTrackingEnd"
    							]
    						},
    						{
    							"$gt" : [
    								"$dtBroadcastEnd",
    								"$dtTrackingStart"
    							]
    						}
    					]
    				}
    			}
    		},
    		{
    			"$project" : {
    				"_id" : false,
    				"dtTrackingStart" : true,
    				"dtTrackingEnd" : true,
    				"dtBroadcastEnd" : true,
    				"dtBroadcastStart" : true,
    				"idDevice" : "$idDevice",
    				"naBroadcast" : "$data.archivo",
    				"naType" : "$data.tipo",
    				"qtBroadcastDurationS" : {
    					"$divide" : [
    						{
    							"$subtract" : [
    								{
    									"$convert" : {
    										"input" : "$dtBroadcastEnd",
    										"to" : {
    											"$const" : "date"
    										}
    									}
    								},
    								{
    									"$convert" : {
    										"input" : "$dtBroadcastStart",
    										"to" : {
    											"$const" : "date"
    										}
    									}
    								}
    							]
    						},
    						{
    							"$const" : 1000
    						}
    					]
    				},
    				"naWeekday" : {
    					"$switch" : {
    						"branches" : [
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$dayOfWeek" : {
    												"date" : {
    													"$convert" : {
    														"input" : "$data.inicio",
    														"to" : {
    															"$const" : "date"
    														}
    													}
    												}
    											}
    										},
    										{
    											"$const" : 1
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Domingo"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$dayOfWeek" : {
    												"date" : {
    													"$convert" : {
    														"input" : "$data.inicio",
    														"to" : {
    															"$const" : "date"
    														}
    													}
    												}
    											}
    										},
    										{
    											"$const" : 2
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Lunes"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$dayOfWeek" : {
    												"date" : {
    													"$convert" : {
    														"input" : "$data.inicio",
    														"to" : {
    															"$const" : "date"
    														}
    													}
    												}
    											}
    										},
    										{
    											"$const" : 3
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Martes"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$dayOfWeek" : {
    												"date" : {
    													"$convert" : {
    														"input" : "$data.inicio",
    														"to" : {
    															"$const" : "date"
    														}
    													}
    												}
    											}
    										},
    										{
    											"$const" : 4
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Miércoles"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$dayOfWeek" : {
    												"date" : {
    													"$convert" : {
    														"input" : "$data.inicio",
    														"to" : {
    															"$const" : "date"
    														}
    													}
    												}
    											}
    										},
    										{
    											"$const" : 5
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Jueves"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$dayOfWeek" : {
    												"date" : {
    													"$convert" : {
    														"input" : "$data.inicio",
    														"to" : {
    															"$const" : "date"
    														}
    													}
    												}
    											}
    										},
    										{
    											"$const" : 6
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Viernes"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$dayOfWeek" : {
    												"date" : {
    													"$convert" : {
    														"input" : "$data.inicio",
    														"to" : {
    															"$const" : "date"
    														}
    													}
    												}
    											}
    										},
    										{
    											"$const" : 7
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Sábado"
    								}
    							}
    						],
    						"default" : {
    							"$const" : "Fecha incorrecta"
    						}
    					}
    				},
    				"naMonth" : {
    					"$switch" : {
    						"branches" : [
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$substrBytes" : [
    												"$data.inicio",
    												{
    													"$const" : 5
    												},
    												{
    													"$const" : 2
    												}
    											]
    										},
    										{
    											"$const" : "01"
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Enero"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$substrBytes" : [
    												"$data.inicio",
    												{
    													"$const" : 5
    												},
    												{
    													"$const" : 2
    												}
    											]
    										},
    										{
    											"$const" : "02"
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Febrero"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$substrBytes" : [
    												"$data.inicio",
    												{
    													"$const" : 5
    												},
    												{
    													"$const" : 2
    												}
    											]
    										},
    										{
    											"$const" : "03"
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Marzo"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$substrBytes" : [
    												"$data.inicio",
    												{
    													"$const" : 5
    												},
    												{
    													"$const" : 2
    												}
    											]
    										},
    										{
    											"$const" : "04"
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Abril"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$substrBytes" : [
    												"$data.inicio",
    												{
    													"$const" : 5
    												},
    												{
    													"$const" : 2
    												}
    											]
    										},
    										{
    											"$const" : "05"
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Mayo"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$substrBytes" : [
    												"$data.inicio",
    												{
    													"$const" : 5
    												},
    												{
    													"$const" : 2
    												}
    											]
    										},
    										{
    											"$const" : "06"
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Junio"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$substrBytes" : [
    												"$data.inicio",
    												{
    													"$const" : 5
    												},
    												{
    													"$const" : 2
    												}
    											]
    										},
    										{
    											"$const" : "07"
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Julio"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$substrBytes" : [
    												"$data.inicio",
    												{
    													"$const" : 5
    												},
    												{
    													"$const" : 2
    												}
    											]
    										},
    										{
    											"$const" : "08"
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Agosto"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$substrBytes" : [
    												"$data.inicio",
    												{
    													"$const" : 5
    												},
    												{
    													"$const" : 2
    												}
    											]
    										},
    										{
    											"$const" : "09"
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Septiembre"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$substrBytes" : [
    												"$data.inicio",
    												{
    													"$const" : 5
    												},
    												{
    													"$const" : 2
    												}
    											]
    										},
    										{
    											"$const" : "10"
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Octubre"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$substrBytes" : [
    												"$data.inicio",
    												{
    													"$const" : 5
    												},
    												{
    													"$const" : 2
    												}
    											]
    										},
    										{
    											"$const" : "11"
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Noviembre"
    								}
    							},
    							{
    								"case" : {
    									"$eq" : [
    										{
    											"$substrBytes" : [
    												"$data.inicio",
    												{
    													"$const" : 5
    												},
    												{
    													"$const" : 2
    												}
    											]
    										},
    										{
    											"$const" : "12"
    										}
    									]
    								},
    								"then" : {
    									"$const" : "Diciembre"
    								}
    							}
    						],
    						"default" : {
    							"$const" : "Fecha incorrecta"
    						}
    					}
    				},
    				"idPerson" : "$array.data.PersonID",
    				"qtFaceDetected" : {
    					"$cond" : [
    						{
    							"$eq" : [
    								"$array.data.FaceInfo.IsDetected",
    								{
    									"$const" : true
    								}
    							]
    						},
    						{
    							"$const" : 1
    						},
    						{
    							"$const" : 0
    						}
    					]
    				},
    				"qtMaleProbability" : "$array.data.FaceInfo.MaleProbability",
    				"qtAge" : "$array.data.FaceInfo.Age",
    				"naEmotion" : "$array.data.FaceInfo.Emotion",
    				"qtGaze" : "$array.data.FaceInfo.numGazes",
    				"qtGazeDurationS" : "$array.data.FaceInfo.gazeTime",
    				"qtFaceDurationS" : "$array.data.LookingDuration",
    				"qtTrackingDurationS" : "$array.data.TrackingDuration",
    				"qtReId" : "$array.data.ReIDInfo.NumReIDs"
    			}
    		}
    	],
    	"serverInfo" : {
    		"host" : "mercury",
    		"port" : 27017,
    		"version" : "4.2.5",
    		"gitVersion" : "2261279b51ea13df08ae708ff278f0679c59dc32"
    	},
    	"ok" : 1
    }

db.sensorsData.getIndexes();

/* 1 */
{
	"v" : 2,
	"key" : {
		"_id" : 1
	},
	"name" : "_id_",
	"ns" : "mercury.sensorsData"
},

/* 2 */
{
	"v" : 2,
	"key" : {
		"idSensor" : 1,
		"idDevice" : 1,
		"dateTime" : 1
	},
	"name" : "idSensor_1_idDevice_1_dateTime_1",
	"ns" : "mercury.sensorsData",
	"background" : false
},

/* 3 */
{
	"v" : 2,
	"key" : {
		"idSensor" : 1
	},
	"name" : "idSensor_1",
	"ns" : "mercury.sensorsData",
	"background" : true
},

/* 4 */
{
	"v" : 2,
	"key" : {
		"idDevice" : 1
	},
	"name" : "idDevice_1",
	"ns" : "mercury.sensorsData",
	"background" : true
}

db.sensorsData.stats();

{
	"ns" : "mercury.sensorsData",
	"size" : 355821356,
	"count" : 720559,
	"avgObjSize" : 493,
	"storageSize" : 2510925824,
	"capped" : false,
	"wiredTiger" : {
		"metadata" : {
			"formatVersion" : 1
		},
		"creationString" : "access_pattern_hint=none,allocation_size=4KB,app_metadata=(formatVersion=1),assert=(commit_timestamp=none,durable_timestamp=none,read_timestamp=none),block_allocation=best,block_compressor=snappy,cache_resident=false,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=false,extractor=,format=btree,huffman_key=,huffman_value=,ignore_in_memory_cache_size=false,immutable=false,internal_item_max=0,internal_key_max=0,internal_key_truncate=true,internal_page_max=4KB,key_format=q,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=32KB,leaf_value_max=64MB,log=(enabled=true),lsm=(auto_throttle=true,bloom=true,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=false,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_custom=(prefix=,start_generation=0,suffix=),merge_max=15,merge_min=0),memory_page_image_max=0,memory_page_max=10m,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=false,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=90,type=file,value_format=u",
		"type" : "file",
		"uri" : "statistics:table:collection-78--2020613348714777639",
		"LSM" : {
			"bloom filter false positives" : 0,
			"bloom filter hits" : 0,
			"bloom filter misses" : 0,
			"bloom filter pages evicted from cache" : 0,
			"bloom filter pages read into cache" : 0,
			"bloom filters in the LSM tree" : 0,
			"chunks in the LSM tree" : 0,
			"highest merge generation in the LSM tree" : 0,
			"queries that could have benefited from a Bloom filter that did not exist" : 0,
			"sleep for LSM checkpoint throttle" : 0,
			"sleep for LSM merge throttle" : 0,
			"total size of bloom filters" : 0
		},
		"block-manager" : {
			"allocations requiring file extension" : 98572,
			"blocks allocated" : 126701,
			"blocks freed" : 114876,
			"checkpoint size" : 56659968,
			"file allocation unit size" : 4096,
			"file bytes available for reuse" : 2454249472,
			"file magic number" : 120897,
			"file major version number" : 1,
			"file size in bytes" : 2510925824,
			"minor version number" : 0
		},
		"btree" : {
			"btree checkpoint generation" : 116101,
			"column-store fixed-size leaf pages" : 0,
			"column-store internal pages" : 0,
			"column-store variable-size RLE encoded values" : 0,
			"column-store variable-size deleted values" : 0,
			"column-store variable-size leaf pages" : 0,
			"fixed-record size" : 0,
			"maximum internal page key size" : 368,
			"maximum internal page size" : 4096,
			"maximum leaf page key size" : 2867,
			"maximum leaf page size" : 32768,
			"maximum leaf page value size" : 67108864,
			"maximum tree depth" : 3,
			"number of key/value pairs" : 0,
			"overflow pages" : 0,
			"pages rewritten by compaction" : 0,
			"row-store empty values" : 0,
			"row-store internal pages" : 0,
			"row-store leaf pages" : 0
		},
		"cache" : {
			"bytes currently in the cache" : 397732134,
			"bytes dirty in the cache cumulative" : 16410583403,
			"bytes read into cache" : 352945299,
			"bytes written from cache" : 4447470099,
			"checkpoint blocked page eviction" : 0,
			"data source pages selected for eviction unable to be evicted" : 74,
			"eviction walk passes of a file" : 13,
			"eviction walk target pages histogram - 0-9" : 0,
			"eviction walk target pages histogram - 10-31" : 2,
			"eviction walk target pages histogram - 128 and higher" : 0,
			"eviction walk target pages histogram - 32-63" : 6,
			"eviction walk target pages histogram - 64-128" : 5,
			"eviction walks abandoned" : 0,
			"eviction walks gave up because they restarted their walk twice" : 13,
			"eviction walks gave up because they saw too many pages and found no candidates" : 0,
			"eviction walks gave up because they saw too many pages and found too few candidates" : 0,
			"eviction walks reached end of tree" : 26,
			"eviction walks started from root of tree" : 13,
			"eviction walks started from saved location in tree" : 0,
			"hazard pointer blocked page eviction" : 74,
			"in-memory page passed criteria to be split" : 899,
			"in-memory page splits" : 448,
			"internal pages evicted" : 0,
			"internal pages split during eviction" : 0,
			"leaf pages split during eviction" : 53,
			"modified pages evicted" : 509,
			"overflow pages read into cache" : 0,
			"page split during eviction deepened the tree" : 0,
			"page written requiring cache overflow records" : 0,
			"pages read into cache" : 2994,
			"pages read into cache after truncate" : 1,
			"pages read into cache after truncate in prepare state" : 0,
			"pages read into cache requiring cache overflow entries" : 0,
			"pages requested from the cache" : 5689344859,
			"pages seen by eviction walk" : 4412,
			"pages written from cache" : 120997,
			"pages written requiring in-memory restoration" : 0,
			"tracked dirty bytes in the cache" : 0,
			"unmodified pages evicted" : 0
		},
		"cache_walk" : {
			"Average difference between current eviction generation when the page was last considered" : 0,
			"Average on-disk page image size seen" : 0,
			"Average time in cache for pages that have been visited by the eviction server" : 0,
			"Average time in cache for pages that have not been visited by the eviction server" : 0,
			"Clean pages currently in cache" : 0,
			"Current eviction generation" : 0,
			"Dirty pages currently in cache" : 0,
			"Entries in the root page" : 0,
			"Internal pages currently in cache" : 0,
			"Leaf pages currently in cache" : 0,
			"Maximum difference between current eviction generation when the page was last considered" : 0,
			"Maximum page size seen" : 0,
			"Minimum on-disk page image size seen" : 0,
			"Number of pages never visited by eviction server" : 0,
			"On-disk page image sizes smaller than a single allocation unit" : 0,
			"Pages created in memory and never written" : 0,
			"Pages currently queued for eviction" : 0,
			"Pages that could not be queued for eviction" : 0,
			"Refs skipped during cache traversal" : 0,
			"Size of the root page" : 0,
			"Total number of pages currently in cache" : 0
		},
		"compression" : {
			"compressed page maximum internal page size prior to compression" : 4096,
			"compressed page maximum leaf page size prior to compression " : 131072,
			"compressed pages read" : 2994,
			"compressed pages written" : 114713,
			"page written failed to compress" : 0,
			"page written was too small to compress" : 6284
		},
		"cursor" : {
			"bulk loaded cursor insert calls" : 0,
			"cache cursors reuse count" : 23117,
			"close calls that result in cache" : 0,
			"create calls" : 133,
			"insert calls" : 923701,
			"insert key and value bytes" : 3698369282,
			"modify" : 0,
			"modify key and value bytes affected" : 0,
			"modify value bytes modified" : 0,
			"next calls" : 41743564417,
			"open cursor count" : 4,
			"operation restarted" : 0,
			"prev calls" : 2,
			"remove calls" : 203142,
			"remove key bytes removed" : 730203,
			"reserve calls" : 0,
			"reset calls" : 3967697491,
			"search calls" : 431822594326,
			"search near calls" : 349251038,
			"truncate calls" : 0,
			"update calls" : 0,
			"update key and value bytes" : 0,
			"update value size change" : 0
		},
		"reconciliation" : {
			"dictionary matches" : 0,
			"fast-path pages deleted" : 0,
			"internal page key bytes discarded using suffix compression" : 309317,
			"internal page multi-block writes" : 2867,
			"internal-page overflow keys" : 0,
			"leaf page key bytes discarded using prefix compression" : 0,
			"leaf page multi-block writes" : 3220,
			"leaf-page overflow keys" : 0,
			"maximum blocks required for a page" : 1,
			"overflow values written" : 0,
			"page checksum matches" : 130720,
			"page reconciliation calls" : 9474,
			"page reconciliation calls for eviction" : 460,
			"pages deleted" : 456
		},
		"session" : {
			"object compaction" : 0
		},
		"transaction" : {
			"update conflicts" : 0
		}
	},
	"nindexes" : 4,
	"indexBuilds" : [ ],
	"totalIndexSize" : 38014976,
	"indexSizes" : {
		"_id_" : 7380992,
		"idSensor_1_idDevice_1_dateTime_1" : 22368256,
		"idSensor_1" : 4153344,
		"idDevice_1" : 4112384
	},
	"scaleFactor" : 1,
	"ok" : 1
}

Forgot to tag you, @Pavel_Duchovny. Thanks in advance for your answer.

Hi @Javier_Blanco,

I think that an $in operator might be better for you, the $or force an uneeded index merge:

"$match": //Deja pasar sólo los archivos de emisiones y de reacciones
		{
		    "idSensor" : { $in : [2,3]}
		}

In general, it seems that your data model might benefit from embedding devices which suppose to be linked opposed to having the $lookup.

Another idea might be to use $filter instead of unwind and $match.

Best
Pavel

1 Like

Hi, @Pavel_Duchovny:

Nothing really changes with $in instead of $or; the query still goes on forever -or at east for a very long time-. The problem lies within the second $match stage; by commenting it, the client just needs a little more than two seconds to prompt back the outcome. But that $match stage is crucial…