I'm consulting my API and I pass as parameters an array of stations
http: // localhost: 3790 / api / getSensorIdstation / 191,1123
The answer I get is the following:
{
"Station_types":[
{
"_id":"5cc85899a0160f16c50f4199",
"marca":"Hortisis",
"modelo":"Estacion",
"fabricante":"Hortisis",
"id_station":[
"191",
"457",
"459",
"463",
"465",
"426",
"424"
],
"sensor_type":{
"name":"2",
"type":"clima",
"place":"interior",
"img":"assets/img/temp.png",
"name_comun":"Temp. Ambiente",
"medida":"ºC",
"interfaz":"greenhouse"
}
},
{
"_id":"5cc85899a0160f16c50f4199",
"marca":"Hortisis",
"modelo":"Estacion",
"fabricante":"Hortisis",
"id_station":[
"191",
"457",
"459",
"463",
"465",
"426",
"424"
],
"sensor_type":{
"name":"3",
"type":"clima",
"place":"interior",
"img":"assets/img/hum.png",
"name_comun":"Hum. Relativa",
"medida":"%",
"interfaz":""
}
},
{
"_id":"5cebaa26c6b02a54c6a3f782",
"marca":"Sigfox 8DF004 CO2 Sensirion (L1 214)",
"modelo":"Sigfox 8DF004 CO2 Sensirion (L1 214)",
"fabricante":"Sigfox 8DF004 CO2 Sensirion (L1 214)",
"id_station":[
"1123"
],
"sensor_type":{
"name":"3",
"type":"clima",
"place":"interior",
"img":"assets/img/hum.png",
"name_comun":"Hum. Relativa",
"medida":"%",
"interfaz":""
}
},
{
"_id":"5cebaa26c6b02a54c6a3f782",
"marca":"Sigfox 8DF004 CO2 Sensirion (L1 214)",
"modelo":"Sigfox 8DF004 CO2 Sensirion (L1 214)",
"fabricante":"Sigfox 8DF004 CO2 Sensirion (L1 214)",
"id_station":[
"1123"
],
"sensor_type":{
"name":"43",
"type":"clima",
"place":"interior",
"img":"assets/img/humidity.png",
"name_comun":"Def. vapor de presión",
"medida":"kPa",
"interfaz":""
}
}
]
}
I would need only the ID that is being consulted in this case on id_station appear in 191 and 1123, not the rest.
I am testing with the filters but it does not work correctly.
This is my code:
function getSensorIdstation(req, res) {
var array = req.params.id_station;
var arr = array.split(',');
Station_types.aggregate([
{ "$match": { "id_station": { "$in": arr } } },
{ "$unwind": "$sensor_type" },
],
(err, Station_types) => {
if (err) return res.status(500).send({ message: 'Error al realizar la peticion' })
if (!Station_types) return res.status(404).send({ message: 'Error el usuario no existe' })
res.status(200).send({ Station_types })
})
}
the result should be this:
{
"Station_types":[
{
"_id":"5cc85899a0160f16c50f4199",
"marca":"Hortisis",
"modelo":"Estacion",
"fabricante":"Hortisis",
"id_station":[
"191"
],
"sensor_type":{
"name":"2",
"type":"clima",
"place":"interior",
"img":"assets/img/temp.png",
"name_comun":"Temp. Ambiente",
"medida":"ºC",
"interfaz":"greenhouse"
}
},
{
"_id":"5cc85899a0160f16c50f4199",
"marca":"Hortisis",
"modelo":"Estacion",
"fabricante":"Hortisis",
"id_station":[
"191"
],
"sensor_type":{
"name":"3",
"type":"clima",
"place":"interior",
"img":"assets/img/hum.png",
"name_comun":"Hum. Relativa",
"medida":"%",
"interfaz":""
}
},
{
"_id":"5cebaa26c6b02a54c6a3f782",
"marca":"Sigfox 8DF004 CO2 Sensirion (L1 214)",
"modelo":"Sigfox 8DF004 CO2 Sensirion (L1 214)",
"fabricante":"Sigfox 8DF004 CO2 Sensirion (L1 214)",
"id_station":[
"1123"
],
"sensor_type":{
"name":"3",
"type":"clima",
"place":"interior",
"img":"assets/img/hum.png",
"name_comun":"Hum. Relativa",
"medida":"%",
"interfaz":""
}
},
{
"_id":"5cebaa26c6b02a54c6a3f782",
"marca":"Sigfox 8DF004 CO2 Sensirion (L1 214)",
"modelo":"Sigfox 8DF004 CO2 Sensirion (L1 214)",
"fabricante":"Sigfox 8DF004 CO2 Sensirion (L1 214)",
"id_station":[
"1123"
],
"sensor_type":{
"name":"43",
"type":"clima",
"place":"interior",
"img":"assets/img/humidity.png",
"name_comun":"Def. vapor de presión",
"medida":"kPa",
"interfaz":""
}
}
]
}
EDIT
add filter code: but it does not return the rest of fields, only the id_station
Station_types.aggregate([
{ "$match": { "id_station": { "$in": arr } } },
{ "$unwind": "$sensor_type" },
{
$project: {
sensor_type: {
$filter: {
input: '$id_station',
as: 'shape',
cond: { $in: ['$$shape', arr] }
}
},
_id: 0
}
}
],
In the end I found the solution after several tests, added a filter within the project and added the rest of the fields.
function getSensorIdstation(req, res) {
var array = req.params.id_station;
console.log(array);
var arr = array.split(',');
console.log(arr);
Station_types.aggregate([
{ "$match": { "id_station": { "$in": arr } } },
{ "$unwind": "$sensor_type" },
{
$project: {
_id: 0,
id_station: {
$filter: {
input: '$id_station',
as: 'shape',
cond: { $in: ['$$shape', arr] }
}
},
marca: "$marca",
modelo: "$modelo",
fabricante: "$fabricante",
sensor_type: {
name: "$sensor_type.name",
type: "$sensor_type.type",
place: "$sensor_type.place",
img: "$sensor_type.img",
name_comun: "$sensor_type.name_comun",
medida: "$sensor_type.medida",
interfaz: "$sensor_type.interfaz"
}
}
}
],
(err, Station_types) => {
if (err) return res.status(500).send({ message: 'Error al realizar la peticion' })
if (!Station_types) return res.status(404).send({ message: 'Error el usuario no existe' })
/*SensorRecuperado.sort(function(a, b) {
return b.name_comun.localeCompare(a.name_comun);
})*/
res.status(200).send({ Station_types })
})
}
Hope that helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.