【发布时间】:2019-10-20 06:33:11
【问题描述】:
我正在咨询我的 API,我将一组站作为参数传递
http://localhost:3790/api/getSensorIdstation/191,1123
我得到的答案如下:
{
"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":""
}
}
]
}
在这种情况下,我只需要在 id_station 上查询的 ID 出现在 191 和 1123 中,而不是其余部分。
我正在使用过滤器进行测试,但它无法正常工作。
这是我的代码:
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 })
})
}
结果应该是这样的:
{
"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":""
}
}
]
}
编辑
添加过滤器代码: 但它不返回其余字段,只返回 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
}
}
],
【问题讨论】:
-
我现在不知道如何解决你的问题,对不起,但我想建议你不要将你的聚合结果命名为“Station_types”,就像你的猫鼬方案一样, 因为你正在覆盖它(仅在这个函数的范围内,但我认为这不是一个好习惯)
-
现在它只显示 ID_station 而不显示其余信息。