There are not enough DateTime operators and math operators to predict the date. But you can create age ranges by composing a dynamic query:
Define date ranges as clipping dates as
dt18 = today - 18 dt25 = today - 25 ... dt65 = today - 65
Then follow the nested conventions in which you gradually use clipping dates as age group markers, for example:
db.folks.save({ "_id" : 1, "bd" : ISODate("2000-02-03T00:00:00Z") }); db.folks.save({ "_id" : 2, "bd" : ISODate("2010-06-07T00:00:00Z") }); db.folks.save({ "_id" : 3, "bd" : ISODate("1990-10-20T00:00:00Z") }); db.folks.save({ "_id" : 4, "bd" : ISODate("1964-09-23T00:00:00Z") }); db.folks.aggregate( { $project: { ageGroup: { $cond: [{ $gt: ["$bd", ISODate("1995-03-19")] }, "age0_18", { $cond: [{ $gt: ["$bd", ISODate("1988-03-19")] }, "age18_25", "age25_plus"] }] } } }, { $group: { _id: "$ageGroup", count: { $sum: 1 } } })
source share