如何根据MongoDB中的字段值找到日期差异?

发布于 2025-01-17 23:52:28 字数 1030 浏览 0 评论 0 原文

我想找到集合中的日期差,但对于与给定条件匹配的对象。 有关如何实现这一目标的任何线索。

目前,我正在使用查询:

db.sales.aggregate([ {
    $project: {
       item: 1,
       dateDifference: {
          $subtract: [ "$enddate", "$startdate" ]
       }
    }
} ])

但这将返回集合中所有对象的日期差。

我想拥有一些诸如说我有一个项目字段之类的东西,并且有多个项目,例如汽车,自行车,周期等。现在,我只需要基于项目价值的日期差异。

假设我们在一个集合中有4个会议,其中包括:

[
  {
    _id: "112233",
    item: "CAR",
    startdate: ISODate("2022-03-16T07:38:08.466Z"),
    enddate: ISODate("2022-03-16T08:38:08.466Z")
  },
  {
    _id: "11222333",
    item: "BIKE",
    startdate: ISODate("2022-02-16T07:38:08.466Z"),
    enddate: ISODate("2022-02-14T08:38:08.466Z")
  },
  {
    _id: "1122333243",
    item: "CAR",
    startdate: ISODate("2022-01-16T07:38:08.466Z"),
    enddate: ISODate("2022-02-16T01:38:08.466Z")
  },
  {
    _id: "12312233",
    item: "BUS",
    startdate: ISODate("2021-03-16T07:38:08.466Z"),
    enddate: ISODate("2021-03-16T08:38:08.466Z")
  }
]

现在我想找到一辆汽车的Startdate和enddate的差异。

I want to find the date difference in a collection but for the objects which matches a given condition.
Any clue on how to achieve this.

Currently, I am using a query:

db.sales.aggregate([ {
    $project: {
       item: 1,
       dateDifference: {
          $subtract: [ "$enddate", "$startdate" ]
       }
    }
} ])

but this will return the date difference for all of my objects in the collection.

I want to have something like say I have a items field and there are multiple items say Car, Bike, Cycle etc. Now i only want the date difference based upon the item value.

say we have 4 sessions in a collection with properties like:

[
  {
    _id: "112233",
    item: "CAR",
    startdate: ISODate("2022-03-16T07:38:08.466Z"),
    enddate: ISODate("2022-03-16T08:38:08.466Z")
  },
  {
    _id: "11222333",
    item: "BIKE",
    startdate: ISODate("2022-02-16T07:38:08.466Z"),
    enddate: ISODate("2022-02-14T08:38:08.466Z")
  },
  {
    _id: "1122333243",
    item: "CAR",
    startdate: ISODate("2022-01-16T07:38:08.466Z"),
    enddate: ISODate("2022-02-16T01:38:08.466Z")
  },
  {
    _id: "12312233",
    item: "BUS",
    startdate: ISODate("2021-03-16T07:38:08.466Z"),
    enddate: ISODate("2021-03-16T08:38:08.466Z")
  }
]

Now i want to find the difference of startdate and enddate say for CAR only.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛起拖把扫天下 2025-01-24 23:52:28

使用 $ expr $ datediff

db.collection.aggregate([
  {
    $match: {
      $expr: {
        $gt: [
          {
            $dateDiff: {
              startDate: "$startdate",
              endDate: "$enddate",
              unit: "minute"
            }
          },
          20
        ]
      }
    }
  }
])

mongoplayground


db.collection.aggregate([
  {
    $match: {
      item: "CAR"
    }
  },
  {
    $set: {
      diff: {
        $dateDiff: {
          startDate: "$startdate",
          endDate: "$enddate",
          unit: "day"
        }
      }
    }
  }
])

Use $expr and $dateDiff

db.collection.aggregate([
  {
    $match: {
      $expr: {
        $gt: [
          {
            $dateDiff: {
              startDate: "$startdate",
              endDate: "$enddate",
              unit: "minute"
            }
          },
          20
        ]
      }
    }
  }
])

mongoplayground


db.collection.aggregate([
  {
    $match: {
      item: "CAR"
    }
  },
  {
    $set: {
      diff: {
        $dateDiff: {
          startDate: "$startdate",
          endDate: "$enddate",
          unit: "day"
        }
      }
    }
  }
])

mongoplayground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文