Mongo 查询某个日期范围内几个月内的最新和最旧数据

问题描述 投票:0回答:1

我有mogo数据:

    {_id: 1, value: 4.0, date: ISODate("2024-01-01T00:00:00.000Z")}
    {_id: 2, value: 6.0, date: ISODate("2024-01-03T00:00:00.000Z")}
    {_id: 3, value: 9.0, date: ISODate("2024-01-08T00:00:00.000Z")}
    {_id: 4, value: 11.0, date: ISODate("2024-01-14T00:00:00.000Z")}
    {_id: 5, value: 16.0, date: ISODate("2024-01-28T00:00:00.000Z")}
    {_id: 6, value: 20.0, date: ISODate("2024-02-03T00:00:00.000Z")}
    {_id: 7, value: 24.0, date: ISODate("2024-02-07T00:00:00.000Z")}
    {_id: 8, value: 30.0, date: ISODate("2024-02-19T00:00:00.000Z")}
    {_id: 9, value: 37.0, date: ISODate("2024-02-25T00:00:00.000Z")}
    {_id: 10, value: 40.0, date: ISODate("2024-03-02T00:00:00.000Z")}
    {_id: 11, value: 44.0, date: ISODate("2024-03-06T00:00:00.000Z")}
    {_id: 12, value: 52.0, date: ISODate("2024-03-10T00:00:00.000Z")}
    {_id: 13, value: 60.0, date: ISODate("2024-03-17T00:00:00.000Z")}
    {_id: 14, value: 60.0, date: ISODate("2024-03-26T00:00:00.000Z")}
    {_id: 15, value: 68.0, date: ISODate("2024-03-29T00:00:00.000Z")}
    {_id: 16, value: 40.0, date: ISODate("2024-04-04T00:00:00.000Z")}

我想在查询结果中接收每个月的日期范围的两个值(最接近月初的值和最接近月末的值)。 示例:我想要数据形式范围 2024.01.01-2024.04.30 然后我想接收:

    {_id: 1, value: 4.0, date: ISODate("2024-01-01T00:00:00.000Z")}
    {_id: 5, value: 16.0, date: ISODate("2024-01-28T00:00:00.000Z")}
    {_id: 6, value: 20.0, date: ISODate("2024-02-03T00:00:00.000Z")}
    {_id: 9, value: 37.0, date: ISODate("2024-02-25T00:00:00.000Z")}
    {_id: 10, value: 40.0, date: ISODate("2024-03-02T00:00:00.000Z")}
    {_id: 15, value: 68.0, date: ISODate("2024-03-29T00:00:00.000Z")}
    {_id: 16, value: 40.0, date: ISODate("2024-04-04T00:00:00.000Z")}
mongodb mongodb-query aggregation-framework
1个回答
0
投票

您可以使用

dayOfMonth
创建一个名为
$dayOfMonth
的辅助字段来获取该月中的相应日期。然后,使用
$setWindowFields
计算当月分区中
$dateTrunc
各自的排名。之后,选择升序排名或降序排名。如果 2 个条目共享同一天,请将
$rank
替换为
$denseRank
以获取两条记录。

db.collection.aggregate([
  {
    "$match": {
      date: {
        $gte: ISODate("2024-01-01"),
        $lte: ISODate("2024-04-30")
      }
    }
  },
  {
    "$set": {
      "dayOfMonth": {
        "$dayOfMonth": "$date"
      }
    }
  },
  {
    "$setWindowFields": {
      "partitionBy": {
        "$dateTrunc": {
          "date": "$date",
          "unit": "month"
        }
      },
      "sortBy": {
        "dayOfMonth": 1
      },
      "output": {
        "ascOrder": {
          $rank: {}
        }
      }
    }
  },
  {
    "$setWindowFields": {
      "partitionBy": {
        "$dateTrunc": {
          "date": "$date",
          "unit": "month"
        }
      },
      "sortBy": {
        "dayOfMonth": -1
      },
      "output": {
        "descOrder": {
          $rank: {}
        }
      }
    }
  },
  {
    "$match": {
      "$expr": {
        "$or": [
          {
            $eq: [
              1,
              "$ascOrder"
            ]
          },
          {
            $eq: [
              1,
              "$descOrder"
            ]
          }
        ]
      }
    }
  },
  {
    "$unset": [
      "ascOrder",
      "dayOfMonth",
      "descOrder"
    ]
  }
])

蒙戈游乐场

© www.soinside.com 2019 - 2024. All rights reserved.