'spring-data-mongodb aggregation with composite ID

I'm having trouble reading documents from MongoDB using the aggregation framework: I always get null IDs in my results. This only happens for documents that have composite IDs. I tried various versions of spring-data-mongodb (1.10.12, 2.0.7), same result.

Entity definition class

    @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;
    }
}

Testing code

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

Output

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

Documents fetched with aggregate: 
null - param1
null - param2

Debugging into the following method MappingMongoConverter.read(final MongoPersistentEntity entity, final Document bson, final ObjectPath path) I found that in the first case (find method) the documentAccessor variable has the following contents

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

whereas in the second case (aggregation query) it looks like

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

The document gets flattened somehow, which makes it impossible for the converter to populate the ID field. I must be doing something wrong, but what?



Solution 1:[1]

Spring Data MongoDB lower than 3.x automatically flatten composite id (fields under composite id are unwrapped and place at root object). This is removed in version 3.0 onwards: https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#new-features.3.0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vu le