728x90
반응형

Aggregation 이용한 Field 시간 계산

use('hmc');

 

// db.test.drop();

 

// db.test.insertOne(

//   {

//     vin : "KNFSTZ76BFK964124",

//     event_time : ISODate("2021-03-16T14:14:45.000Z"),

//     system_time : ISODate("2021-03-16T14:14:49.000Z")

//   }

// )

 

db.test.aggregate(

    {

        $match:

          {

              vin : "KNFSTZ76BFK964124" ,

              event_time : { "$gte" : ISODate("2021-03-16T14:14:40Z"), "$lt" : ISODate("2021-03-16T14:14:50Z") }

          }

    },

    {

        $project:

        {

            _id:0,

            vin : 1,

            event_time : 1,

            system_time : 1,

            timeDiff : {

                $cond: {

                  if: { $gte: ["$system_time", "$event_time"] },

                  then: { $subtract: [ "$system_time", "$event_time" ] },

                  else : 0

                }

            }

        }

    },

    {$match : {timeDiff:{$gte:0}}}

)

system_time event_time 차이가 1 이상 나는 데이터 조회

use('hmc');

 

let timeDiff = {

   $cond: {

      if: { $gte: ["$system_time", "$event_time"] },

      then: { $subtract: [ "$system_time", "$event_time" ] },

      else : 0

   }

};

 

  db.test.find(

  {

    vin : "KNFSTZ76BFK964124" ,

    event_time : { "$gte" : ISODate("2021-03-16T14:14:40Z"), "$lt" : ISODate("2021-03-16T14:14:50Z") },

    $expr: { $gte:[ timeDiff,  NumberDecimal("1000") ] }

  }

  ,{_id:0, vin : 1, event_time : 1, system_time : 1}

)

반응형

+ Recent posts