0

I'm trying to get the list of posts for which the publish date is equal or less than the current date.

I'm using NestJS, Mongo & typeORM; which syntax should I use?

const posts =
  await this.mbRepository.find(
    { where: { "deletedAt": null , "publishDate" <= currentDate } }
  );
nenissa
  • 3
  • 3
  • 1
    Does this answer your question? [return query based on date](https://stackoverflow.com/questions/8835757/return-query-based-on-date) – gurisko Jan 06 '21 at 12:36

1 Answers1

0

    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 } } });

Get current day and make two point. One as begining of the day and another as end of the day. Then you can use mongodb operator $gte and $lt. In this way you will get all post which have been publish in this time range.

Mritunjay Upadhyay
  • 1,004
  • 1
  • 15
  • 27