【问题标题】:How to retrieve data from MongoDB using Spring - group by latest datetime for each day?如何使用 Spring 从 MongoDB 检索数据 - 按每天的最新日期时间分组?
【发布时间】:2020-07-31 23:32:15
【问题描述】:

我在 MongoDB 中有以下格式的文档。我想提取每天按类别、名称、日期(格式如 yyyy-mm-dd)分组的数据,以了解通过或失败的状态。

{ "name":"Scenario number one", "category":"general", "creationDateTime":"2020-06-15T00:18:35.608+00:00", "status":"Passed" },
{ "name":"Scenario number one", "category":"general", "creationDateTime":"2020-06-15T00:11:35.608+00:00", "status":"Failed" },
{ "name":"Scenario number one", "category":"general", "creationDateTime":"2020-06-15T00:08:35.608+00:00", "status":"Passed" },
{ "name":"Scenario number two", "category":"happy-path", "creationDateTime":"2020-06-15T00:17:35.608+00:00", "status":"Failed" },
{ "name":"Scenario number one", "category":"general", "creationDateTime":"2020-06-13T00:12:35.608+00:00", "status":"Passed" },
{ "name":"Scenario number two", "category":"happy-path", "creationDateTime":"2020-06-12T00:11:35.608+00:00", "status":"Failed" },
{ "name":"Scenario number two", "category":"happy-path", "creationDateTime":"2020-06-12T00:10:35.608+00:00", "status":"Passed" }

我想以如下格式输出

{"category":"general", "name":"Scenario number one", "date":"2020-06-15", "status":"Passed"}
{"category":"happy-path", "name":"Scenario number two", "date":"2020-06-15", "status":"Failed"}
{"category":"general", "name":"Scenario number one", "date":"2020-06-13", "status":"Failed"}
{"category":"happy-path", "name":"Scenario number two", "date":"2020-06-12", "status":"Failed"}

请帮助使用 Spring Code。谢谢!

【问题讨论】:

  • 分组不过滤。按天分组意味着使用每一天的所有文档。
  • 按时间戳排序,将时间戳转换为日期,按日期分组,使用$last获取每个日期的最新文档。

标签: mongodb aggregation-framework spring-data-mongodb


【解决方案1】:

Mongo 查询类似于

[
  {
    $sort: {
      "creationDateTime": -1
    }
  },
  {
    "$addFields": {
      "date": {
        "$substr": [
          "$creationDateTime",
          0,
          10
        ]
      }
    }
  },
  {
    "$group": {
      "_id": {
        name: "$name",
        cat: "$category",
        cdt: "$date"
      },
      "category": {
        $first: "$category"
      },
      status: {
        $first: "$status"
      },
      name: {
        $first: "$name"
      },
      date: {
        $first: "$date"
      }
    }
  },
  {
    $project: {
      _id: 0
    }
  }
]

工作Mongo playground。而Spring中的聚合,

 public List<Object> test() {
        Aggregation aggregation = Aggregation.newAggregation(
                sort(Sort.Direction.DESC, "creationDateTime"),
                a -> new Document("$addFields",
                        new Document("date",
                                new Document("$substr", Arrays.asList("$creationDateTime", 0, 10))
                        )
                ),
                group(Fields.from(
                        Fields.field("name", "$name"),
                        Fields.field("category", "$category"),
                        Fields.field("date", "$date")
                ))
                        .first("category").as("category")
                        .first("status").as("status")
                        .first("name").as("name")
                        .first("date").as("date"),
                project().andExclude("_id")

        ).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());

        return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();

    }

我没有测试它,但它应该可以工作,see this too

【讨论】:

  • 感谢您的帮助!我有以 ISO 格式存储的时间戳。在从时间戳中提取之前,我需要将时间戳转换为具有夏令时的 CST。那有可能吗?原因是插入记录的实际日期时间是 CST。由于仅基于 ISO 日期检索时结果不准确。
  • 有可能,我没试过。参考docs.mongodb.com/manual/tutorial/model-time-data。如果您在搜索中没有发现任何有用的信息,请提出问题
  • 我使用了上下文 -> new Document("$addFields", new Document("date", DateOperators.DateToString.dateOf("creationDateTime").toString("%m-%d-%Y ").toDocument(context))) 而不是 context -> new Document("$addFields", new Document("date", new Document("$substr", Arrays.asList("$creationDateTime", 0, 10) )))
猜你喜欢
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-18
相关资源
最近更新 更多