OK, here it goes the whole query:
var pipeline =
[
{
"$match":
{
"idSensor": 3,
"idDevice": {"$in": db.devices.distinct("_id", {"group.$id": 11})},
}
},
{
"$lookup":
{
"from": "devices",
"localField": "idDevice",
"foreignField": "_id",
"as": "array"
}
},
{
"$unwind": "$array"
},
{
"$project":
{
"_id": 1,
"idLocation": {"$toString": "$array.idLocation"},
"coNaLocation": {"$ifNull": ["$array.locationAddress", null]},
"idDevice": {"$toString": "$idDevice"},
"naDay": {"$substrBytes": ["$data.inicio", 0, 10]},
"coInterval":
{
"$concat":
[
"[",
{"$substrBytes": ["$data.inicio", 11, 2]},
":00, ",
{"$dateToString": {"format": "%H", "date": {"$add": [{"$toDate": "$data.inicio"}, 3600000]}, "timezone": "$dateTime.offset"}},
":00]"
]
},
"naBroadcast": "$data.archivo",
"naType": "$data.tipo",
"qtBroadcast": {"$toInt": 1},
"qtBroadcastDurationS": {"$divide": [{"$subtract": [{"$toDate": "$data.fin"}, {"$toDate": "$data.inicio"}]}, 1000]}
}
}
]
db.sensorsDataHistoric.aggregate(pipeline)
In NoSQL Booster, it needs less than 5 seconds to retrieve the documents. First one:
{
"_id": "612f55683e1fd4728b578a16",
"idLocation": null,
"coNaLocation": null,
"idDevice": "2443",
"naDay": "2021-08-04",
"coInterval": "[10:00, 11:00]",
"naBroadcast": " mym_covidMEDIDAS_2021_fer.mp4",
"naType": "video",
"qtBroadcast": 1,
"qtBroadcastDurationS": 31
}
The problem: idDevice
cannot be properly formatted as a JSON, so I can’t use the query in my BI software.
Note: I only upload the piece of code between brackets to my BI software. It doesn’t allow anything else. It has another formulary to fill in the rest of parameters: server, db, collection, etc.
I have an alternative query that produces the same outcome:
var pipeline =
[
{
"$match": {"group.$id": 11}
},
{
"$lookup":
{
"from": "sensorsDataHistoric",
"localField": "_id",
"foreignField": "idDevice",
"as": "array"
}
},
{
"$unwind": "$array"
},
{
"$match": {"array.idSensor": 3}
},
{
"$project":
{
"_id": "$array._id",
"idLocation": {"$toString": "$idLocation"},
"coNaLocation": {"$ifNull": ["$locationAddress", null]},
"idDevice": {"$toString": "$array.idDevice"},
"naDay": {"$substrBytes": ["$array.data.inicio", 0, 10]},
"coInterval":
{
"$concat":
[
"[",
{"$substrBytes": ["$array.data.inicio", 11, 2]},
":00, ",
{"$dateToString": {"format": "%H", "date": {"$add": [{"$toDate": "$array.data.inicio"}, 3600000]}, "timezone": "$array.dateTime.offset"}},
":00]"
]
},
"naBroadcast": "$array.data.archivo",
"naType": "$array.data.tipo",
"qtBroadcast": {"$toInt": 1},
"qtBroadcastDurationS": {"$divide": [{"$subtract": [{"$toDate": "$array.data.fin"}, {"$toDate": "$array.data.inicio"}]}, 1000]}
}
}
]
db.devices.aggregate(pipeline)
But it needs more than 700 s to run. I’m uploading a big bunch of documents to my BI software, so I need the process to be quick. First one:
{
"_id": "612b8450959e931c0727e32d",
"idLocation": "2415",
"coNaLocation": "[0032] Calle Fray Ceferino, 25 - 33001 - Oviedo (Asturias)",
"idDevice": "2353",
"naDay": "2021-08-29",
"coInterval": "[12:00, 13:00]",
"naBroadcast": " APP_masymas100000_MAY21.mp4",
"naType": "video",
"qtBroadcast": 1,
"qtBroadcastDurationS": 25
}
That’s why I’m trying to find a proper syntax for
"idDevice": {"$in": db.devices.distinct("_id", {"group.$id": 11})}
I thought
"idDevice": {"$in": ["distinct": "devices", "key": "_id", "query": {"group.$id": 11}]}
would be an equivalent one, but it seems it’s not, not sure why.