MongoDB comparing dates only without times

前端 未结 5 1425
南旧
南旧 2020-12-11 15:15

How do I query for a date field in MongoDB with the date only, and not times? For example, if MongoDB stores July 7, 2015 with any time, I want to match that day o

5条回答
  •  眼角桃花
    2020-12-11 15:33

    You can extract the date as string at any given format and compare it with your date at that format using aggregation pipiline

    $addFields: { "creationDate":  {$dateToString:{format: "%Y-%m-%d", date: "$createdAt"}}}},
    {$match :  { creationDate:  {$eq: req.query.date}}
    

提交回复
热议问题