33

I have this query to get results on month. But I wanted to get the results of today.

var start = new Date(2010, 11, 1);
var end = new Date(2010, 11, 30);

db.posts.find({created_on: {$gte: start, $lt: end}});

What is the best way to do this?

chridam
  • 100,957
  • 23
  • 236
  • 235
Rex Adrivan
  • 993
  • 1
  • 10
  • 23
  • `new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – Prinzhorn Mar 29 '15 at 08:48

3 Answers3

93

Your start date object should hold the current date time hours at 00:00:00.000 (milliseconds precision) and set the hours for today's date to 23:59:59.999:

var start = new Date();
start.setHours(0,0,0,0);

var end = new Date();
end.setHours(23,59,59,999);

Then pass the modified date objects as usual in your MongoDB query operator:

db.posts.find({created_on: {$gte: start, $lt: end}});

If you are using the dayjs date utility library, this can be done by using the startOf() and endOf() methods on dayjs current date object, passing the string 'day' as argument:

const start = dayjs().startOf('day'); // set to 12:00 am today
const end = dayjs().endOf('day'); // set to 23:59 pm today

You can also use $expr as follows:

db.posts.find({
    $expr: {
        $eq: [
            { $dateToString: { format: '%Y-%m-%d', date: '$$NOW' } },
            { $dateToString: { format: '%Y-%m-%d', date: '$created_on' } },
        ],
    },
})
chridam
  • 100,957
  • 23
  • 236
  • 235
  • I had to use the .format() method on the dayjs object so the Mongo driver could handle it (I'm not using Mongoose). For example: const time = dayjs().startOf('day').format() – szeb May 16 '22 at 15:27
2

Solution

To find the today date results or data between dates, use the following:

NodeJS:

// Date format YYYY-MM-DD
// Suppose you want to look for 1 day data between 2021-12-27 00:00:00 and 2021-12-28 00:00:00
let start = new Date("2021-12-27");
let end = new Date("2021-12-28");

// "created_on" can be any field with a date 
db.posts.find({created_on: {$gte: start, $lt: end}});

This works on the Mongo Shell too:

db.posts.find({created_on: {$gte: new ISODate("2021-12-27T00:00:00.000Z"), $lt: new ISODate("2021-12-28T00:00:00.000Z")}})

Another Way:

// 27th half day
db.posts.find({created_on: {$gte: new Date("2021-12-27T00:00:00.000Z"), $lt: new Date("2021-12-27T12:00:00.000Z")}})

Reference

find - Select / filter data

$gte - Greater Than / Equal

$lt - Lower Than

Stas Sorokin
  • 3,029
  • 26
  • 18
0
// Get Today Start Date and End Date
  let startOfDay = new Date();
  startOfDay.setHours(0, 0, 0, 0);
  let endOfDay = new Date();
  endOfDay.setHours(23, 59, 59, 999);

  // Get Yesterday Start Date and End Date
  let startOfYesterday = new Date();
  startOfYesterday.setDate(startOfYesterday.getDate() - 1);
  startOfYesterday.setHours(0, 0, 0, 0);
  let endOfYesterday = new Date();
  endOfYesterday.setDate(endOfYesterday.getDate() - 1);
  endOfYesterday.setHours(23, 59, 59, 999);

  // Get Last Month Start Date and End Date
  let startOfPreviousMonth = new Date();
  startOfPreviousMonth.setMonth(startOfPreviousMonth.getMonth() - 1);
  startOfPreviousMonth.setDate(1);
  startOfPreviousMonth.setHours(0, 0, 0, 0);
  let endOfPreviousMonth = new Date();
  endOfPreviousMonth.setMonth(endOfPreviousMonth.getMonth() - 1);
  let lastDayOfPreviousMonth = new Date(
    endOfPreviousMonth.getFullYear(),
    endOfPreviousMonth.getMonth() + 1,
    0
  );
  endOfPreviousMonth.setDate(lastDayOfPreviousMonth.getDate());
  endOfPreviousMonth.setHours(23, 59, 59, 999);

Mohit Saud
  • 95
  • 1
  • 5