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

时间:2018-05-16 12:29:06

标签: spring mongodb aggregation

我在使用聚合框架从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;
    }
}
  

测试代码

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

输出

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

Documents fetched with aggregate: 
null - param1
null - param2

调试到以下方法MappingMongoConverter.read(最终的MongoPersistentEntity实体,最终的Document bson,最终的ObjectPath路径)我发现在第一种情况下(find方法)documentAccessor变量具有以下内容

  

文件{{_ id =文件{{stringKey = 1,intKey = 1}},param = param1,_class = MyEntity}}

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

  

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

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

0 个答案:

没有答案