Querying embedded documents by matching dates in MongoDB

て烟熏妆下的殇ゞ 提交于 2019-12-04 18:13:55

You need not make use of regex here, something simpler like this should work:

db.posts.find({"headers.Date": new Date(2001, 06, 06) })

This should work if the dates you saved in the DB are without time (just day, month, year)

Now if you have dates saved with new Date(), which includes the time components as well, then you need to create a date range which includes all the moments for that day :

db.posts.find( //query for all moments/time of a specific date
  {"headers.Date": {"$gte": new Date(2001, 6, 6), "$lt": new Date(2001, 6, 7)}})

Note - The API for Date is Date(YYYY,MM,DD) and counting for 'month' starts from '0' and counting for 'date' starts from '1'.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!