如何在MONGODB中计算当天最大值与前一天最大值之间的差值

qjp7pelc  于 2022-11-22  发布在  Go
关注(0)|答案(1)|浏览(133)
{
metadata:{
dat:jkjcsvbdskjcbdskjcbdac,
meterId:kahcvajc
}
activeEnergy:1111,
actualtime:1689827191000
}

文档是这样的,我只面临问题的activeEnergy,所以我想只关注这一点。下面我写了代码,在第一组对象中,我按年,月,日划分,但在实际代码中,它是动态的,如果我从前端接收的有效负载是月和周,那么我相应地分组,但如果我在有效负载中接收的是日,那么我按小时分组,我计算该小时的最大和最小能量,然后对所有小时求和,因为activeEnergy是连续的,但问题是我不是每秒都得到数据,所以我可能在上午10:25得到第一个数据,在上午10:45得到最后一个数据。因此,通过取最大和最小值,我只计算了这20分钟,错过了所有剩余时间的数据。理想情况下,我应该做的是用前一小时的最大值来计算这一小时的最大值。同样,对于当天,第一个文档是上午12:59,最后一个数据是晚上11点,如果我计算当天的最大和最小值,我仍然会错过大约2小时的数据,所以我必须找到一种方法来找到今天的最大值和前一天的最大值的差异。这成为一

db.ts_events.aggregate([
    {
        $project: {
        "y":{"$year": {$toDate: "$actualtime"}},
            "m":{"$month": {$toDate: "$actualtime"}},
            "d":{"$dayOfMonth": {$toDate: "$actualtime"}},
            "h":{"$hour": {$toDate: "$actualtime"}},
            "activeEnergy": 1,
            "metadata.meterId": 1,
            "activePower": 1,
            "actualtime": 1,
      
            "powerFactor": 1,
            "metadata.dat": 1
        }
    },


    {
        $match: {
            "metadata.dat": "62f0f3459731692a5eab5ad6/south0tpbit/tamilnadu5dvs8w/chennaidzc2yd/kknagarj4ffzo",
            "actualtime": {
                $gte: 1656613800000, $lte: 1659292199999,
            },
            //          "metadata.device":"ObjectId(62f0f9b5f757672222282d9)" how to check using object id?,
            "metadata.meterId": "911615402222257_2",
        }
    },
    {
        $group: {
            _id: {
                date: {
                    year: "$y",
                    month: "$m",
                    day: "$d",
//                    hour: "$h",
                },
                meter: "$metadata.meterId",
            },
            maxValue: {
                $max: "$activeEnergy"
            },
            minValue: {
                $min: "$activeEnergy"
            },
            averageActivePowerOfDay: { $avg: "$activePower" },
            averagePowerFactorOfDay: { $avg: "$powerFactor" },
        }
    },
    {
        $addFields: {
            differnce: {
                $subtract: [
                    "$maxValue",
                    "$minValue"
                ]
            },
        }
    },
    //
    //
    //
    {
        $group: {
            _id: null, res: {
                $push: '$$ROOT'
            }, differnceSum: {
                $sum: '$differnce'
            },
            averageActivePowerOverThePeriod: {
                $avg: "$averageActivePowerOfDay"
            },
            averagePowerFactorOverThePeriod: {
                $avg: "$averagePowerFactorOfDay"
            }
        }
    }
])
g6ll5ycj

g6ll5ycj1#

您可以尝试使用$setWindowFields

db.collection.aggregate([
   { $set: { actualTime: { $toDate: "$actualtime" } } },
   {
      $setWindowFields: {
         partitionBy: "$metadata.dat",
         sortBy: { actualTime: 1 },
         output: {
            max_today: {
               $max: "$activeEnergy",
               window: { range: [-1, 1], unit: "day" }
            },
            max_yesterday: {
               $max: "$activeEnergy",
               window: { range: [-2, -1], unit: "day" }
            },
            day: {
               $last: "$actualTime",
               window: { range: [-1, 1], unit: "day" }
            },
         }
      }
   },
   {
      $group: {
         _id: { metadata: "$metadata", day: "$day" },
         values: {
            $addToSet: {
               max_today: "$max_today",
               max_yesterday: "$max_yesterday"
            }
         }
      }
   },
   { $replaceWith: { $mergeObjects: ["$_id", { $first: "$values" }] } }
])

相关问题