简体   繁体   English

Spring Data MongoDB 中的分组在映射到具有复合键的类时返回 NULL _id

[英]Grouping in Spring Data MongoDB returns NULL _id when mapping to a class with composite key

I want to aggregate a collection of documents that match certain conditions, group them and map the output to a different class object.我想聚合一组符合特定条件的文档,将它们分组并将输出映射到不同的类对象。 The aggregation works fine and I get the expected total but the _id field is always NULL.聚合工作正常,我得到了预期的total_id字段始终为 NULL。

I'm using spring-data-mongodb 2.1.11 and MongoDB 3.6.我正在使用 spring-data-mongodb 2.1.11 和 MongoDB 3.6。

This is the class to be aggregated:这是要聚合的类:

@Document
public class LegOrder {

    public static class Key {
        @Indexed
        long itemId;

        long transactionId;
        ...
    }

    @Id
    private Key id;

    @Indexed
    private long brandId;

    private int units;
    ...
}

This is the aggregation output class:这是聚合输出类:

@Document
public class ItemAggregation {

    public static class Key {
        @Indexed
        long itemId;

        @Indexed
        long brandId;
    }

    @Id
    private Key id;

    private long total;
    ...
}

My aggregation method:我的聚合方法:

public ItemAggregation aggregate(long itemId, long brandId) {
    MatchOperation matchStage = Aggregation.match(new Criteria().andOperator(
            Criteria.where("id.itemId").is(itemId),
            Criteria.where("brandId").is(brandId)
    ));

    GroupOperation groupStage = Aggregation.group("id.itemId", "brandId")
            .sum("units").as("total")
            ...
            ;

    Aggregation aggregation = Aggregation.newAggregation(matchStage, groupStage);

    return mongoTemplate.aggregate(aggregation, LegOrder.class, ItemAggregation.class).getUniqueMappedResult();
}

The executed query in MongoDB:在 MongoDB 中执行的查询:

[
  {
    "$match": {
      "$and": [
        { "_id.itemId": 1 },
        { "brandId": 2}
        ]
    }
  },
  {
    "$group": {
      "_id": {
        "itemId": "$_id.itemId",
        "brandId": "$brandId"
      },
      "total": { "$sum": "$units" }
    }
  }
]

If I run this query in the mongo shell the _id field is properly populated.如果我在 mongo shell 中运行此查询,则会正确填充_id字段。

Any idea how to achieve it?知道如何实现它吗?

Thank you谢谢

Sorry for the late response.回复晚了非常抱歉。 I faced this issue now and found this solution.我现在遇到了这个问题并找到了这个解决方案。 My aggregation output in console is我在控制台中的聚合输出是

{
        "_id" : {
                "ownerId" : BinData(3,"xkB0S9Wsktm+tSKBruv6og=="),
                "groupbyF" : "height"
        },
        "docs" : [
                {
                        "id" : ObjectId("5fe75026e211c50ef5741b31"),
                        "aDate" : ISODate("2020-12-26T15:00:51.056Z"),
                        "value" : "rrr"
                }
        ]
}
{
        "_id" : {
                "ownerId" : BinData(3,"AAAAAAAAAAAAAAAAAAAAAA=="),
                "groupbyF" : "weight"
        },
        "docs" : [
                {
                        "id" : ObjectId("5fe6f702e211c50ef5741b2f"),
                        "aDate" : ISODate("2020-12-26T08:40:28.742Z"),
                        "value" : "55"
                },
                {
                        "id" : ObjectId("5fe6f6ade211c50ef5741b2e"),
                        "aDate" : ISODate("2020-12-26T08:38:58.098Z"),
                        "value" : "22"
                }
        ]
}

The mapping that worked for me对我有用的映射

import lombok.Data;

@Data
public class AggregationLatest2Type{
    private String ownerId;
    private String key;
    private List<Doc> docs;
    
    @Data
    public  class Doc{
            private String _id;
            private Date aDate;
            private String value;
            
            
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM