-1

How to get Today, Upcoming and Past Birthdays using MongoDB by passing days range like next 60 days Upcoming Birthdays or Past 60 days Birthdays etc.

We've saved user date of birth in the users collection with below json format.

"dob" : {
    "year" : "2015",
    "month" : "06",
    "day" : "30"
},

"birthday" : ISODate("1975-08-26T18:30:00.000Z") // Correct date format

Basically my solution is here in the Right Answer but not clear how to use with my collection dataset.

Find whether someone got a birthday in the next 30 days with mongo

Thanks!

Community
  • 1
  • 1
Vinod Patidar
  • 685
  • 4
  • 17
  • 1
    How is that unclear? The only real difference there is that the "birthday" is stored as a real `Date` object. And you should then also consider that your "strings" are therefore the wrong way to store this information. – Blakes Seven Aug 25 '15 at 13:58
  • Actually i'm not familiar with MongoDB how to use can you please write using my dataset and please give me so let me try. appreciate for your great support! – Vinod Patidar Aug 25 '15 at 14:03
  • Then you should get familiar with the main thing I said. Which is your "strings" are of no use here. Convert the type to a real `Date` object. – Blakes Seven Aug 25 '15 at 14:11
  • below columns in the my collection "name" : 1, "birthday" : 1, Also i'm getting below error: "errmsg" : "exception: can't convert from BSON type String to Date", – Vinod Patidar Aug 25 '15 at 14:16
  • I've "users" collections and info saved as in below format: { "username" : "test", "email" : "test@example.com", "fname" : "Vinod", "lname" : "Patidar", "dob" : { "year" : "1986", "month" : "28", "day" : "08" } } – Vinod Patidar Aug 25 '15 at 14:21
  • 1
    possible duplicate of [Find whether someone got a birthday in the next 30 days with mongo](http://stackoverflow.com/questions/22041785/find-whether-someone-got-a-birthday-in-the-next-30-days-with-mongo) – Blakes Seven Aug 25 '15 at 14:21

1 Answers1

2

In the "users" collection birthday date should be stored as in below format:

"birthday" : ISODate("1975-08-26T18:30:00.000Z")

var today = new Date();
var m1 = { "$match" : {  "birthday" : { "$exists" : true, "$ne" : '' } } }; 
var p1 = { 
    "$project" : {
        "_id" : 0,
        "username" : 1,
        "birthday" : 1,
        "todayDayOfYear" : { "$dayOfYear" : today }, 
        "dayOfYear" : { "$dayOfYear" : "$birthday" }
    } 
};
var p2 = { 
    "$project" : {
        "username" : 1,
        "birthday" : 1,
        "daysTillBirthday" : { "$subtract" : [
             { "$add" : [ 
                     "$dayOfYear",
             { "$cond" : [{"$lt":["$dayOfYear","$todayDayOfYear"]},365,0 ] }
             ] },
             "$todayDayOfYear"
        ] }
    } 
};

if ( showcase == 'today' ) {
    var m2 = { "$match" : {  "daysTillBirthday" : { "$lt" : 1 } } }; // lt:1 = Today Birthdays
} else if ( showcase == 'upcoming' ) {
    var m2 = { "$match" : {  "daysTillBirthday" : { "$lt" : 60 } } }; // lt:60 = Next 60 days Upcoming Birthdays 
} else if ( showcase == 'past' ) {
    var m2 = { "$match" : {  "daysTillBirthday" : { "$gt" : 60 } } }; // gt = Past 60 days Birthdays
}
db.users.aggregate([m1, p1, p2, m2]);
Vinod Patidar
  • 685
  • 4
  • 17