Query on Date object

Hi,

Here is smaple data

{
    "date":ISODate("2022-03-23T15:33:15.551Z")
}
{
    "date":ISODate("2022-03-24T15:19:15.551Z")
}
{
    "date":ISODate("2022-03-25T20:33:15.551Z")
}
{
    "date":ISODate("2022-03-26T20:33:15.551Z")
}
{
    "date":ISODate("2022-03-27T20:33:15.551Z")
}

Will i be able to query date field just by date without time ?
Example check if document with date 2022-03-26 matches ? If yes how can do that ?

Most simple would be to use date ranges, greater than or equal the check date without a time component and less than the next day:

myDate:{$gte:ISODate("2022-03-23T00:00:00.000Z"), $lt:ISODate("2022-03-24T00:00:00.000Z")}

You could project the data field to remove the time and then compare to that, but that would remove any benefits of indexes as you would be checking a calculated value.

Hi John,

My focus is just store only date (yyyy-mm-dd). Unfortunately there is no way to store just date object in mongodb. Even if i store document with Date object with time.

{
    "date":ISODate("2022-03-27T20:33:15.551Z")
}

I am really looking to check if any way exists to check if incoming date in query matches the document date. Just date (yyyy-mm-dd) and ignoring time.

I do not want to use range because i am looking for one specific date, my ask is very specific if Date (2023-08-12) if found do something. Any suggestions ?

Strip the times out before you save them then and then you can just query on a simple date object.

/Edit and to add to the above, a date range does not have to be multiple dates, it can be all times within ONE day, as per the example above.

1 Like