【问题标题】:How do I GroupBy in Spring Data Mongodb without Aggregation?如何在没有聚合的 Spring Data Mongodb 中进行 GroupBy?
【发布时间】:2017-02-17 07:47:44
【问题描述】:

我有如下数据,我想按类型对数据进行分组,我使用的是spring-data-mongodb

[
    {
        "_id" : ObjectId("58a5518aace6132a88309d98"),
        "type" : "SMS",
    },
    {
        "_id" : ObjectId("58a5518bace6132a88309d99"),
        "type" : "PUSH_NOTIFICATION",
    },
    {
        "_id" : ObjectId("58a5519aace6132a0094d7df"),
        "type" : "SMS",
    },
    {
        "_id" : ObjectId("58a5519aace6132a0094d7e0"),
        "type" : "PUSH_NOTIFICATION",
    }
]

我在用这个方法,不行。

GroupByResults<Queuing> results = mongoTemplate.group("queuing",
                GroupBy.key("type"), Queuing.class);

任何人都知道使用spring-data-mongodb 进行此分组的最佳且清晰的方法。

谢谢。

【问题讨论】:

    标签: java spring mongodb spring-mvc spring-data-mongodb


    【解决方案1】:

    这是group 操作的正确语法。

    GroupByResults<Queuing> results = mongoTemplate.group("queuing",
                GroupBy.key("type").initialDocument("{}").reduceFunction("function(doc, prev) {}"),
                Queuing.class);
    

    更多信息在这里http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.group.example

    【讨论】:

      猜你喜欢
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2014-01-30
      • 2019-11-27
      • 1970-01-01
      相关资源
      最近更新 更多