【问题标题】:how to add 7 days in date manually using mongodb(aggregation)?如何使用 mongodb(聚合)手动添加 7 天的日期?
【发布时间】:2023-02-16 18:55:50
【问题描述】:

我有一个查询,其中我使用开始日期和结束日期获取数据。它工作得很好。现在我想使用聚合在我的开始日期中添加 7 天,并希望获取开始日期 + 7 添加天的数据。我试过下面的代码但没有成功

{
            $match: {
              publishedAt: {
                $lt: new Date(startDate),
                $gte: new Date(endDate),
              },
            },
          },
          {
            $set: {
              publishedAt: {
                $add: [new Date(startDate), 1000 * 60 * 60 * 24],
              },
            },
          },

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework aggregation nosql-aggregation


    【解决方案1】:

    用这个:

       {
          $set: {
             publishedAt: {
                $dateAdd: {
                   startDate: new Date(startDate),
                   unit: "day",
                   amount: 7
                }
             }
          }
       }
    

    或者使用第三方库,例如Luxon

       {
          $set: {
             publishedAt: DateTime.now().plus({ days: 7 }).toJSDate()
          }
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多