0

I have following types of documents in mongodb.

{
    "id" : 1,
    "type" : "temp",
    "cdate" : "2017-08-10T06:49:12Z"
},
{
    "id" : 2,
    "type" : "test",
    "cdate" : "2017-06-15T06:49:16Z"
},
{
    "id" : 3,
    "type" : "cricket",
    "cdate" : "2017-07-10T06:49:20Z"
},
{
    "id" : 4,
    "type" : "rcon",
    "cdate" : "2017-06-20T06:49:22Z"
},
{
    "id" : 5,
    "type" : "health",
    "cdate" : "2017-09-05T06:49:25Z"
},
{
    "id" : 6,
    "type" : "report",
    "cdate" : "2017-08-10T06:49:29Z"
},
{
    "id" : 7,
    "type" : "normal",
    "cdate" : "2017-08-10T06:49:39Z"
}

From above documents i have cdate in ISO format.I want to filter cdate on month-date base, not on the year.we can do this query in MySQL via DATE_FORMAT(cdate,'%m-%d') .Below is a MySQL query that wants to convert into MongoDB query.

SELECT * FROM `temp`
      WHERE DATE_FORMAT(cdate,'%m-%d') 
      BETWEEN DATE_FORMAT('2022-06-01','%m-%d') and 
      DATE_FORMAT('2022-08-15','%m-%d');

kindly let me know is there any way or any operator like date_format in mongodb so that I can make the same query like above MySQL query.

5a01d01P
  • 663
  • 2
  • 9
  • 20
  • this question is filter on ONLY month-date not year in ISO date key. – 5a01d01P Aug 10 '17 at 09:29
  • @Neil Lunn.Is there any way in mongodb like mysql have "DATE_FORMAT(cdate,'%m-%d')"? – 5a01d01P Aug 10 '17 at 12:19
  • It was not really the right thing to do in mysql is more to the point. The correct thing to do has always been to select between natural values. What you are asking for actually "calculated" values there and it would do the same with MongoDB. Calculation is not good since it cannot use an index. There is no reason why you simply cannot supply the start and end dates for a given month. That's what a "range" query is, and that is the way it's done. – Neil Lunn Aug 10 '17 at 12:22
  • let me explain some scenario.Suppose in above documents,user birthday data is stored and i want to fetch birthday of user that are between 29-Jun to 03-July..i.e. query only match on date-month on "cdate" key not year. – 5a01d01P Aug 10 '17 at 12:32

0 Answers0