Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

282
Views
I want to calculate the average value of iolements label Ext Voltage and Battery Voltage each per month as per created_date,

I'm new in node js and mongodb and now i want to calculate ioelements Ext voltage and battery voltage average value per month where the date can be taken from created_at, i've posted the sample data coming from mongodb below

{
    "recieved_at": "2022-01-05T03:28:07.816Z",
    "data": {
        "timestamp": "2022-01-05T03:28:05.000Z",
        "priority": 0,
        "gps": {
            "longitude": 8.7472616,
            "latitude": 50.0881766,
            "altitude": 171,
            "angle": 179,
            "satellites": 5,
            "speed": 6
        },
        "event_id": 0,
        "properties_count": 6,
        "ioElements": [{
                "id": 239,
                "value": 0,
                "label": "Ignition",
                "valueHuman": "No"
            },
            {
                "id": 240,
                "value": 0,
                "label": "Movement",
                "valueHuman": "No"
            },
            {
                "id": 66,
                "value": 12064,
                "label": "Ext Voltage",
                "dimension": "mV",
                "valueHuman": ""
            },
            {
                "id": 24,
                "value": 6,
                "label": "Speed",
                "dimension": "km/h",
                "valueHuman": ""
            },
            {
                "id": 67,
                "value": 4081,
                "label": "Battery Voltage",
                "dimension": "mV",
                "valueHuman": ""
            },
            {
                "id": 199,
                "value": 6,
                "label": "Trip Odometer",
                "valueHuman": ""
            }
        ]
    },
    "imei": "357073294061474",
    "deviceTcpConnection": {},
    "deviceUdpConnection": {
        "address": "185.116.158.53",
        "port": 10190
    },
    "deviceDetail": true,
    "protocol": "UDP",
    "created_at" => "2022-01-13T08:55:47.984Z"
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You want to be using $group on the month ( that you can extract using $month), lastly we can use the $avg operator to calculate the average of values with ease, like so:

db.collection.aggregate([
  {
    $addFields: {
      extVoltage: {
        "$arrayElemAt": [
          {
            $filter: {
              input: "$data.ioElements",
              as: "elem",
              cond: {
                $eq: [
                  "$$elem.label",
                  "Ext Voltage"
                ]
              }
            }
          },
          0
        ]
      },
      batteryVoltage: {
        "$arrayElemAt": [
          {
            $filter: {
              input: "$data.ioElements",
              as: "elem",
              cond: {
                $eq: [
                  "$$elem.label",
                  "Battery Voltage"
                ]
              }
            }
          },
          0
        ]
      }
    }
  },
  {
    $group: {
      _id: {
        year: {
          "$year": "$created_at"
        },
        month: {
          "$month": "$created_at"
        }
      },
      ExtVoltage: {
        $avg: {
          $ifNull: [
            "$extVoltage.value",
            0
          ]
        }
      },
      BatteryVoltage: {
        $avg: {
          $ifNull: [
            "$batteryVoltage.value",
            0
          ]
        }
      }
    }
  }
])

Mongo Playground

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!