如何用 Mongodb 比较两个日期(使用 NestJS 查找查询)

How to compare two dates with Mongodb (find query using NestJS)

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

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

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

    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。这样你就可以得到在这个时间范围内发布的所有post。