简体   繁体   English

如何在 $group 内减去 mongoDb 聚合中的两个日期?

[英]How to subtract two dates in mongoDb aggregate inside $group?

I am trying to calculate login time of user based on data in mongoDB whenever user login.每当用户登录时,我都试图根据 mongoDB 中的数据计算用户的登录时间。 I have login startDateTime , endDateTime and duration fields but if duration is 0 then it should take the difference of current date time and startDateTime .我有登录startDateTimeendDateTimeduration字段,但如果持续时间为 0,那么它应该取当前日期时间和startDateTime的差值。

The problem I got here is result is wrong because I have checked by subtracting the same dates in browser console using new Date() .我在这里遇到的问题是结果是错误的,因为我已经通过使用new Date()在浏览器控制台中减去相同的日期进行了检查。

Here is the aggregate query I am trying这是我正在尝试的聚合查询

    let queryFilter = {
    createdAt: {
      "$gte": filter.from ? new Date(filter.from) : new Date(new Date().toDateString()),
      "$lte": filter.to ? new Date(filter.to) : new Date(new Date().toDateString())
    }
  }


AgentJournal.aggregate([
    {
      $match: queryFilter
    },
    {
      $group: {
        _id: { agent: "$agent", queue: "$queue", channel: "$channel" },
        loginTime: { "$sum": { "$cond": [{ "$eq": ["$event", "LOGIN"] }, { "$cond": [{ "$eq": ["$duration", 0] }, { $subtract: [new Date(), new Date("$startDateTime")] }, "$duration"] }, 0] } },
      }
    }
  ])

This is a sample document stored in my database:这是存储在我的数据库中的示例文档:

{
    "_id" : ObjectId("608a6ee3e781511b7283c393"),
    "duration" : 0,
    "agent" : "SIP/intellicon",
    "queue" : "sales",
    "event" : "LOGIN",
    "startDateTime" : "2021-04-29 13:27:21",  //String Type
    "endDateTime" : "0000-00-00 00:00:00",    //String Type
    "channel" : "fb_pages",
    "__v" : 0,
    "createdAt" : ISODate("2021-04-29T08:31:31.995Z"),
    "updatedAt" : ISODate("2021-04-29T08:31:31.995Z")
}

Note: I have checked by subtracting integers it works accurate so I think my query is OK注意:我已经通过减去整数来检查它是否准确,所以我认为我的查询没问题

Try $toDate operator to convert your string date to date type,尝试$toDate运算符将您的字符串日期转换为日期类型,

{
  $subtract: [
    new Date(),
    { $toDate: "$startDateTime" }
  ]
}

Playground操场

i have resolved it by using the $toDate told by @turivishal as well as convert the new Date to my local standard time first (ie new Date().toLocaleString() ) and then pass it to $toDate (ie {$toDate:new Date().toLocaleString} )我已经通过使用@turivishal 告诉的$toDate解决了它,并首先将新日期转换为我的本地标准时间(即new Date().toLocaleString() ),然后将其传递给$toDate (即{$toDate:new Date().toLocaleString}

now the query to calculate login time is现在计算登录时间的查询是

loginTime: { "$sum": { "$cond": [{ "$eq": ["$event", "LOGIN"] }, { "$cond": [{ "$eq": ["$duration", 0] }, { $subtract: [{ $toDate: new Date().toLocaleString() }, { $toDate: "$startDateTime" }] }, "$duration"] }, 0] } },

Thanks Again @turivishal再次感谢@turivishal

https://docs.mongodb.com/manual/reference/operator/aggregation/dateDiff/ https://docs.mongodb.com/manual/reference/operator/aggregation/dateDiff/

{
   $dateDiff: {
      startDate: <Expression>,
      endDate: <Expression>,
      unit: <Expression>,
      timezone: <tzExpression>,
      startOfWeek: <String>
   }
}

subtract value return in milliseconds以毫秒为单位减去值返回

  const avgTime = await Activity.aggregate<{
    count?: string;
    avg_time: string;
  }>([
    {
      $match: {
        finishedAt: {
          $exists: true,
        },
      },
    },
    {
      $unwind: '$tags',
    },
    {
      $match: {
        $or: t,
      },
    },
    {
      $group: {
        _id: null,
        count: {
          $sum: 1,
        },
        avg_time: {
          $avg: {
            // $subtract: [
            //   {
            //     $ifNull: ['$finishedAt', 0],
            //   },
            //   {
            //     $ifNull: ['$createdAt', 0],
            //   },
            // ],
            $subtract: ['$finishedAt', '$createdAt'],
            //$subtract: [{ $toDate: '$finishedAt' }, { $toDate: '$createdAt' }],
          },
        },
      },
    },
  ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM