MongoDB:聚合查询未在函数内传递值

发布于 2025-01-10 03:18:36 字数 866 浏览 0 评论 0原文

我面临 Mongoose 聚合查询的问题。我有以下架构,它是一个对象数组,包含 endDate 值。

[
  {
    "id": 1,
    "endDate": "2022-02-28T19:00:00.000Z"
  },
  {
    "id": 2,
    "endDate": "2022-02-24T19:00:00.000Z"
  },
  {
    "id": 3,
    "endDate": "2022-02-25T19:00:00.000Z"
  }
]

因此,在聚合结果期间,我必须添加一个新字段名称 isPast,它包含布尔值,并执行计算以检查 endDate 是否通过。如果已经通过,则 isPast 将为 true,否则为 false

我正在使用 moment 库中的 isBefore 函数,该函数返回布尔值。但在这个函数内部面临着传递 endDate 值的问题。 $endDate 作为字符串传递,而不是值。

有没有办法在函数内传递 endDate 的值?

const todayDate = moment(new Date()).format("YYYY-MM-DD");

db.collection.aggregate([
  {
    $addFields: {
      "isPast": moment('$endDate', 'YYYY-MM-DD').isBefore(todayDate)
    },

  },

])

I am facing a problem with the Mongoose aggregation query. I have the following schema which is an array of objects and contains the endDate value.

[
  {
    "id": 1,
    "endDate": "2022-02-28T19:00:00.000Z"
  },
  {
    "id": 2,
    "endDate": "2022-02-24T19:00:00.000Z"
  },
  {
    "id": 3,
    "endDate": "2022-02-25T19:00:00.000Z"
  }
]

So, during the aggregation result, I have to add a new field name isPast, It contains the boolean value, and perform the calculation to check if the endDate is passed or not. If it is already passed, then isPast will be true otherwise false.

I am using the isBefore function from the moment library which returns the boolean. But inside this function facing a problem regarding passing the endDate value. $endDate is passing as a string, not a value.

Is there a way to pass the value of endDate inside the function?

const todayDate = moment(new Date()).format("YYYY-MM-DD");

db.collection.aggregate([
  {
    $addFields: {
      "isPast": moment('$endDate', 'YYYY-MM-DD').isBefore(todayDate)
    },

  },

])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不奢求什么 2025-01-17 03:18:36

无需 momentjs 即可实现。使用 $toDate 将日期时间字符串转换为日期

db.collection.aggregate([
  {
    $addFields: {
      "isPast": {
        $gt: [
          new Date(),
          {
            $toDate: "$endDate"
          }
        ]
      }
    }
  }
])

示例 Mongo Playground


如果您只想比较日期:

db.collection.aggregate([
  {
    $set: {
      "currentDate": "$NOW",
      "endDate": {
        $toDate: "$endDate"
      }
    }
  },
  {
    $addFields: {
      "isPast": {
        $gt: [
          {
            "$dateFromParts": {
              "year": {
                $year: "$currentDate"
              },
              "month": {
                $month: "$currentDate"
              },
              "day": {
                "$dayOfMonth": "$currentDate"
              }
            }
          },
          {
            "$dateFromParts": {
              "year": {
                $year: "$endDate"
              },
              "month": {
                $month: "$endDate"
              },
              "day": {
                "$dayOfMonth": "$endDate"
              }
            }
          }
        ]
      }
    }
  }
])

Sample Mongo Playground(仅比较日期)

You can achieve without momentjs. Use $toDate to convert date-time string to date

db.collection.aggregate([
  {
    $addFields: {
      "isPast": {
        $gt: [
          new Date(),
          {
            $toDate: "$endDate"
          }
        ]
      }
    }
  }
])

Sample Mongo Playground


If you just want to compare for date only:

db.collection.aggregate([
  {
    $set: {
      "currentDate": "$NOW",
      "endDate": {
        $toDate: "$endDate"
      }
    }
  },
  {
    $addFields: {
      "isPast": {
        $gt: [
          {
            "$dateFromParts": {
              "year": {
                $year: "$currentDate"
              },
              "month": {
                $month: "$currentDate"
              },
              "day": {
                "$dayOfMonth": "$currentDate"
              }
            }
          },
          {
            "$dateFromParts": {
              "year": {
                $year: "$endDate"
              },
              "month": {
                $month: "$endDate"
              },
              "day": {
                "$dayOfMonth": "$endDate"
              }
            }
          }
        ]
      }
    }
  }
])

Sample Mongo Playground (Compare date only)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文