Spring 数据 mongo:当路径包含 HashMap 的键时投影不起作用

Spring data mongo: Projection not working when path includes a key of a HashMap

问题

当我尝试投射一个 java.util.Map 内的值时,我得到了以下异常。但是当我 运行 在 roboMongo 中生成 shell 查询时,它就起作用了。如果有人能指出问题,我将不胜感激。

org.springframework.data.mapping.context.InvalidPersistentPropertyPath: No property Germany found on com.nntn.corona.snapshot.repo.model.StatWithDelta!

查询代码在Java

spring 引导父级: 2.0.5.RELEASE

Criteria matchCriteria = Criteria.where("timestamp").gte(startDate);
        MatchOperation match = Aggregation.match(matchCriteria);
        SortOperation sort = sort(new Sort(Sort.Direction.ASC, "timestamp"));

        // @formatter:off
        ProjectionOperation projection = project()
                .andExpression("timestamp").as("timestamp")
                .andExpression("countries.germany.total").as("total")
                .andExpression("countries.germany.today").as("today");
        // @formatter:on

        Aggregation aggregation = newAggregation(match, sort,projection);

        AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, SnapshotEntry.class,
                Document.class);
        return result.getMappedResults();

数据模型

Java表示法

@Document(collection = "snpashots")
public class SnapshotEntry {

    @Id
    private String id;

    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @Temporal(TemporalType.TIMESTAMP)
    private DateTime timestamp;

    private Map<String, StatWithDelta> countries;
    private StatEntity total;
    private StatEntity today;
    private String source;
    private String previousRecordId;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class StatWithDelta {
        private StatEntity total;
        private StatEntity today;
    }

}

Json表示法

{
    "_id" : "21-03-2020",
    "timestamp" : ISODate("2020-03-21T09:26:00.965Z"),
    "countries" : {
        "germany" : {
            "total" : {
                "born" : NumberLong(81008),
                "dead" : NumberLong(3255),
                "sick" : NumberLong(30000)
            },
            "today" : {
                "born" : NumberLong(50),
                "dead" : NumberLong(10),
                "sick" : NumberLong(12)
            }
        }
    },
    "_class" : "com.nntn.snapshot.repo.model.SnapshotEntry"
}

问题出在TypedAggregation。这是一个特殊的聚合,其中 Spring 包含输入聚合类型的信息。

为避免这种情况,请使用原始聚合(就像您在 MongoDB shell 中 运行 一样):

AggregationResults<SnapshotEntry> result = mongoTemplate.aggregate(aggregation, 
            mongoTemplate.getCollectionName(SnapshotEntry.class),
            SnapshotEntry.class);