簡體   English   中英

Spring Data MongoDB 中的分組在映射到具有復合鍵的類時返回 NULL _id

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

我想聚合一組符合特定條件的文檔,將它們分組並將輸出映射到不同的類對象。 聚合工作正常,我得到了預期的total_id字段始終為 NULL。

我正在使用 spring-data-mongodb 2.1.11 和 MongoDB 3.6。

這是要聚合的類:

@Document
public class LegOrder {

    public static class Key {
        @Indexed
        long itemId;

        long transactionId;
        ...
    }

    @Id
    private Key id;

    @Indexed
    private long brandId;

    private int units;
    ...
}

這是聚合輸出類:

@Document
public class ItemAggregation {

    public static class Key {
        @Indexed
        long itemId;

        @Indexed
        long brandId;
    }

    @Id
    private Key id;

    private long total;
    ...
}

我的聚合方法:

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();
}

在 MongoDB 中執行的查詢:

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

如果我在 mongo shell 中運行此查詢,則會正確填充_id字段。

知道如何實現它嗎?

謝謝

回復晚了非常抱歉。 我現在遇到了這個問題並找到了這個解決方案。 我在控制台中的聚合輸出是

{
        "_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"
                }
        ]
}

對我有用的映射

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