0

I am working with a mongodb aggregation and I would need to replace the part of my object array to an array concatenation. I understand that the part that I have to modify would be push but it is added as objects and I am not very clear how to concatenate that part

This is my current aggregation:

Datagreenhouse.aggregate([
        { "$match": { "id_sensor_station_absolute": { "$in": arr }, "recvTime": { $gte: fechainicio, $lte: fechafin } } }, //, "id_station": { "$in": id_station }
        {
            "$lookup": {
                "from": "station_types",
                "localField": "id_station", // local field in measurements collection
                "foreignField": "id_station", //foreign field from sensors collection
                "as": "sensor"
            }
        },
        { "$unwind": "$sensor" },
        {
            "$addFields": {
                "sensor.attrValue": "$attrValue", // Add attrValue to the sensors
                "sensor.recvTime": "$recvTime", // Add attrName to the sensors
                "sensor.marca": "$sensor.marca",
                "sensor.sensor_type": {
                    $filter: {
                        input: '$sensor.sensor_type',
                        as: 'shape',
                        cond: { $eq: ['$$shape.name', '$attrName'] },
                    }
                }
            }
        },
        {
            "$group": {
                "_id": {
                    "name_comun": "$sensor.sensor_type.name_comun",
                    "name_comun2": "$sensor.marca"
                }, // Group by time

                "data": {
                    "$push": {
                        "name": "$sensor.recvTime",
                        "value": "$sensor.attrValue",
                    },
                },
            }
        },
        {
            "$project": {
                "name": {
                    $reduce: {
                        input: [
                            ["$_id.name_comun2"]
                        ],
                        initialValue: "$_id.name_comun",
                        in: { $concatArrays: ["$$value", "$$this"] }
                    }
                },
                "_id": 0,
                "data": 1
            }
        }
    ]

The current output of this aggregation is:

[
   {
      "data":[
         {
            "name":"2020-06-04T14:30:50.00Z",
            "value":69.4
         },
         {
            "name":"2020-06-04T14:13:31.00Z",
            "value":68.9
         }
      ],
      "name":[
         "Hum. Relativa",
         "Hortisys"
      ]
   },
   {
      "data":[
         {
            "name":"2020-06-04T14:30:50.00Z",
            "value":25.5
         },
         {
            "name":"2020-06-04T14:13:31.00Z",
            "value":26.6
         }
      ],
      "name":[
         "Temp. Ambiente",
         "Hortisys"
      ]
   }
]

I need to change the format of data for data: [[], [], []] Example:

[
       {
          "data":[
             
               ["2020-06-04T14:30:50.00Z",69.4],
                              
               ["2020-06-04T14:13:31.00Z",68.9],
          "name":[
             "Hum. Relativa",
             "Hortisys"
          ]
       },
       {
          "data":[
             
               ["2020-06-04T14:30:50.00Z",69.4],
                              
               ["2020-06-04T14:13:31.00Z",68.9],
          "name":[
             "Temp. Ambiente",
             "Hortisys"
          ]
       }
]

1 Answer 1

1

The $push operator won't accept an array directly, but you can give it another operator that returns an array, like

  data:{ $push:{ $concatArrays:[[ "$sensor.recvTime", "$sensor.attrValue" ]]}},
Sign up to request clarification or add additional context in comments.

1 Comment

Hello, sorry to bother you, would it be possible to multiply the value of recvTime x 1000 before departure?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.