【问题标题】:How to compare two dates with Mongodb (find query using NestJS)如何使用 Mongodb 比较两个日期(使用 NestJS 查找查询)
【发布时间】:2021-04-12 04:33:24
【问题描述】:

我正在尝试获取发布日期等于或小于当前日期的帖子列表。

我正在使用 NestJS、Mongo 和 typeORM;我应该使用哪种语法?

const posts =
  await this.mbRepository.find(
    { where: { "deletedAt": null , "publishDate" <= currentDate } }
  );

【问题讨论】:

标签: mongodb request find


【解决方案1】:

    let firstPoint = new Date();
    firstPoint.setHours(0);
    firstPoint.setMinutes(0);
    firstPoint.setSeconds(0);
    let lastPoint = new Date();
    lastPoint.setHours(23);
    lastPoint.setMinutes(59);
    lastPoint.setSeconds(59);

    const posts = await this.mbRepository.find({ publishDate: { $gte: firstPoint, $lt: lastPoint } } });

获取当前日期并说明两点。一个作为一天的开始,另一个作为一天的结束。然后你可以使用 mongodb 运算符$gte$lt。这样你就可以得到所有在这个时间范围内发布的帖子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 2011-04-15
    • 2014-08-01
    • 2020-04-14
    相关资源
    最近更新 更多