Concat mongodb数组

时间:2019-06-05 13:23:43

标签: node.js mongodb mongoose

我正在使用Mongodb和附加组件。我需要连接两个字段id_station和sensor_type.name

因为两者都是数组,所以我正在做过滤器,以消除与输入属性不对应的其余元素:

这是我的代码:

function getSensorIdstationType(req, res) {
    let type = req.params.type;
    var array = req.params.id_station;
    var arr = array.split(',');

    Station_types.aggregate([
            { "$match": { "id_station": { "$in": arr }, 'sensor_type.type': type } },
            //{ "$unwind": "$id_station" },

            {
                $project: {
                    sensor_type: {
                        $filter: {
                            input: '$sensor_type',
                            as: 'shape',
                            cond: { $eq: ['$$shape.type', type] }
                        },
                    },
                    _id: 0,
                    id_station: {
                        $filter: {
                            input: '$id_station',
                            as: 'shape',
                            cond: { $in: ['$$shape', arr] }
                        },
                    },
                    pruebas: { $concat: ["shape", " _ ", 'id_station'] }
                },

            },


        ],
        (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 })
        })
}

我的数据示例:

{
"sensor_type": [
{
"name": "11_X_X_600_avg",
"type": "clima",
"place": "interior",
"img": "assets/img/contrast.png",
"name_comun": "Radiación solar",
"medida": "W/m2",
"interfaz": ""
}
],
"id_station": [
"012080F3"
],
"pruebas": "shape - id_station"
}

我需要它出现在字段中,尝试输入值11_X_X_600_avg_012080F3

当我尝试进行连接时,出现错误,并且我不知道如何正确执行

pruebas: { $concat: ["$sensor_type.name", " _ ", '$id_station'] }

0 个答案:

没有答案