1

In mongodb I have datetime field like "2018-01-26T16:40:19.305+05:00" How I will filter with today's date only?. I mean without time?

My document:

{
    "pk": NumberLong("6585118137"),
    "created_time": ISODate("2018-01-26T16:40:19.305+05:00"),
}

I want something like

select * from table where date(created_time) = "2018-01-26"
Saifullah khan
  • 686
  • 11
  • 19

1 Answers1

2

we can compare the date is $gte new Date("2018-01-26") and $lt new Date("2018-01-27")

db.col.find( 
    {$expr : 
        { $and: [ { $gte : ["$created_time", new Date("2018-01-26")] }, { $lt : [ "$created_time", new Date("2018-01-27") ] } ] } 
    } 
)

or format date to %Y%m%d (yyyyMMdd) and compare with date string to be searched

db.col.find(
    {$expr : 
        {$eq :
            [{$dateToString: {format: '%Y%m%d',date: "$created_time"}}, "20180126"]
        }
    }
)
Saravana
  • 12,647
  • 2
  • 39
  • 57