首页 > 解决方案 > MongoDb 过滤器获取两个日期之间的对象

问题描述

我正在尝试使用 MongoDB 在两个日期之间获取通知;两个日期之间有 7 天,并使用下面的函数获取数据,但我总是错误代码 500,知道如何解决吗?

下面添加了示例数据。

功能

exports.getNotifications = async (req, res) => {
  try {
    const notifications = await notification.find({
      createdAt: { $gte: ISODate(date), $lt: ISODate(expires) },
    });
    return res.status(200).json({ notifications });
  } catch (error) {
    return res.status(500).json({
      ...error,
    });
  }
};

样本数据

  {
"_id": {
"$oid": "613200ad493dae19e881e031"
},
"date": {
"$date": "2021-09-03T11:01:47.972Z"
},
"expireDate": {
"$date": "2021-09-10T11:01:47.972Z"
},
"seenBy": [],
"title": "Social Karanlık Mod!",
"coverImage": "https://www.linkpicture.com/q/Karanlık-Mod-1.png",
"actionType": "basic",
"context": "Profil sekmesindeki butondan karanlık ve aydınlık mod arasında geçiş yapabilirsiniz!",
"__v": 0
}

标签: javascriptnode.jsmongodb

解决方案


推荐阅读