具有复合 ID 的 spring-data-mongodb 聚合

Gui*_*m S 6 spring aggregation mongodb

我在使用聚合框架从 MongoDB 读取文档时遇到问题:我的结果中总是出现空 ID。这仅适用于具有复合 ID 的文档。我尝试了各种版本的 spring-data-mongodb (1.10.12, 2.0.7),结果相同。

实体定义类

    @Document(collection="entities")
    public class MyEntity {
    static class CompositeKey implements Serializable {
        private String stringKey;

        private Integer intKey;

        public CompositeKey(String stringKey, Integer intKey) {
            this.stringKey = stringKey;
            this.intKey = intKey;
        }

        public Integer getIntKey() {
            return intKey;
        }

        public String getStringKey() {
            return stringKey;
        }

        public String toString() {
            return "{" + stringKey + " - " + intKey + "}";
        }
    }

    @Id
    private CompositeKey id;

    private String param;

    public MyEntity() {}

    public MyEntity(String stringKey, Integer intKey) {
        id = new CompositeKey(stringKey, intKey);
    }

    public CompositeKey getId(){
        return id;
    }

    public void setId(CompositeKey id) {
        this.id = id;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }
}
Run Code Online (Sandbox Code Playgroud)

测试代码

public static void main(String[] args) {        
    MongoClient client = new MongoClient("127.0.0.1");
    SimpleMongoDbFactory factory = new SimpleMongoDbFactory(client, "aggTest");
    MongoTemplate mongoTemplate = new MongoTemplate(factory);

    MyEntity entity = new MyEntity();
    entity.setId(new MyEntity.CompositeKey("one", 1));
    entity.setParam("param1");
    mongoTemplate.save(entity);

    entity = new MyEntity();
    entity.setId(new MyEntity.CompositeKey("two", 2));
    entity.setParam("param2");
    mongoTemplate.save(entity);

    Criteria crit = Criteria.where("param").ne("param3");
    List<AggregationOperation> aggOpList = new ArrayList<AggregationOperation>();
    aggOpList.add(Aggregation.match(crit));

    System.out.println("Documents fetched with find: ");        
    for (MyEntity aggResult : mongoTemplate.find(new Query(crit), MyEntity.class).toArray(new MyEntity[0]))
        System.out.println(aggResult.getId() + " - " + aggResult.getParam());

    System.out.println("\nDocuments fetched with aggregate: ");        
    TypedAggregation<MyEntity> aggregation = new TypedAggregation<>(MyEntity.class, aggOpList);
    AggregationResults<MyEntity> aggregate = mongoTemplate.aggregate(aggregation, MyEntity.class);      
    for (MyEntity aggResult : aggregate.getMappedResults())
        System.out.println(aggResult.getId() + " - " + aggResult.getParam());       
}
Run Code Online (Sandbox Code Playgroud)

输出

Documents fetched with find: 
{one - 1} - param1
{two - 2} - param2

Documents fetched with aggregate: 
null - param1
null - param2
Run Code Online (Sandbox Code Playgroud)

调试成如下方法MappingMongoConverter.read(final MongoPersistentEntity entity, final Document bson, final ObjectPath path)发现在第一种情况下(find方法)documentAccessor变量有如下内容

文档{{_id=文档{{stringKey=one, intKey=1}}, param=param1, _class=MyEntity}}

而在第二种情况下(聚合查询)它看起来像

文档{{stringKey=one, intKey=1, param=param1, _class=MyEntity}}

文档以某种方式变平,这使得转换器无法填充 ID 字段。我一定是做错了什么,但什么?

小智 0

Spring Data MongoDB 低于 3.x 会自动展平复合 id(复合 id 下的字段被展开并放置在根对象中)。这在 3.0 版本中被删除: https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#new-features.3.0