Mongodb 从日期数组中查找即将到来的生日

Mongodb 从日期数组中查找即将到来的生日,mongodb,mongoose,Mongodb,Mongoose,我有一系列像childrendob一样的日期 [ "2000-01-21T15:18:49+05:30", "2008-03-12T15:18:49+05:30" ] 我想从users表中获取数据,其中孩子的生日是从今天起7天后。 假设今天是1月15日,结果应该是childrendob为21-1的所有用户 我需要mongodb查询此 从你的客户那里得到7天后的一个月和一天 $filter要迭代日期数组的循环,请从月份和日期检查$和

我有一系列像childrendob一样的日期

[
    "2000-01-21T15:18:49+05:30",
     "2008-03-12T15:18:49+05:30"
 ]
我想从users表中获取数据,其中孩子的生日是从今天起7天后。 假设今天是1月15日,结果应该是childrendob为21-1的所有用户 我需要mongodb查询此

  • 从你的客户那里得到7天后的一个月和一天
  • $filter
    要迭代
    日期
    数组的循环,请从月份和日期检查
    $和
    条件
  • $toDate
    若要将字符串日期转换为ISO日期,它已在ISO日期中,则可以跳过/删除此对话
  • $month
    从日期开始获取月份
  • $dayOfMonth
    从日期中获取日期
  • 日期的
    $match
    不应为空

嗨,如果我解释错了,很抱歉。我需要mongo查询this@saching请浏览此链接@sachin为什么它在数组中,一个孩子有2个或更多的出生日期?@turivishal但是用户可以有多个孩子。这些日期在集合中是ISO日期格式的?我的答案需要修改,我删除了它。这可能会导致错误。既然你发了,我就不再发了。非常感谢。
let date = new Date();
date.setDate(date.getDate() + 7); // add 7 days in current date
let month = date.getMonth() + 1; // month
let day = date.getDate(); // date
db.collection.aggregate([
  {
    $project: {
      dates: {
        $filter: {
          input: "$dates",
          cond: {
            $and: [
              { $eq: [{ $month: { $toDate: "$$this" } }, month] }, // input month variable
              { $eq: [{ $dayOfMonth: { $toDate: "$$this" } }, day] } // input day variable
            ]
          }
        }
      }
    }
  },
  { $match: { dates: { $ne: [] } } }
])