spring-projects / spring-projects/spring-data-mongodb

Invalid path reference after lookup

Open
#5,011 3 comments 0 reactions 1 assignee View on GitHub

@mp911de is already working on this.

Since Jul 14, 2025.

status: feedback-provided status: waiting-for-triage
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Hello. We recently upgraded from spring-boot-starter-parent 3.1.1 to 3.5.3 and suddenly encountered this issue.
person in this case is reference to a collection in our database.

Code:

    LookupOperation lookupOperation1 = Aggregation.lookup("person", "person.$id", "_id", "person");
    operations.add(lookupOperation1);

    Criteria criteria = new Criteria();

    criteria = criteria.andOperator(
            Criteria.where("type").is("normal"),
            Criteria.where("person.deleted").is(true)
    );

    MatchOperation matchOperation2 = Aggregation.match(criteria);
    operations.add(matchOperation2);

    GroupOperation groupOperation3 = Aggregation.group("_id").count().as("count");

    Aggregation aggregation = Aggregation.newAggregation(lookupOperation1, matchOperation2, groupOperation3);
    AggregationResults<DBObject> countResults = mongoTemplate.aggregate(aggregation, Address.class, DBObject.class);`

Result:
org.springframework.data.mapping.MappingException: Invalid path reference person.deleted; Associations can only be pointed to directly or via their id property

at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.getPath(QueryMapper.java:1274)
at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.<init>(QueryMapper.java:1134)
at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.<init>(QueryMapper.java:1111)
at org.springframework.data.mongodb.core.convert.QueryMapper.createPropertyField(QueryMapper.java:365)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:155)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedKeyword(QueryMapper.java:386)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:128)
at org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext.getMappedObject(TypeBasedAggregationOperationContext.java:82)
at org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext.getMappedObject(TypeBasedAggregationOperationContext.java:77)
at org.springframework.data.mongodb.core.aggregation.InheritingExposedFieldsAggregationOperationContext.getMappedObject(InheritingExposedFieldsAggregationOperationContext.java:50)
at org.springframework.data.mongodb.core.aggregation.MatchOperation.toDocument(MatchOperation.java:74)
at org.springframework.data.mongodb.core.aggregation.AggregationOperation.toPipelineStages(AggregationOperation.java:55)
at org.springframework.data.mongodb.core.aggregation.AggregationOperationRenderer.toDocument(AggregationOperationRenderer.java:56)
at org.springframework.data.mongodb.core.aggregation.AggregationPipeline.toDocuments(AggregationPipeline.java:86)
at org.springframework.data.mongodb.core.aggregation.Aggregation.toPipeline(Aggregation.java:756)
at org.springframework.data.mongodb.core.AggregationUtil.createPipeline(AggregationUtil.java:98)
at org.springframework.data.mongodb.core.MongoTemplate.doAggregate(MongoTemplate.java:2135)
at org.springframework.data.mongodb.core.MongoTemplate.doAggregate(MongoTemplate.java:2110)
at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:2104)
at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:2019)`

However when we change lookupOperation1 to Aggregation.lookup("person", "person.$id", "_id", "p"); and the criteria to criteria = criteria.andOperator( Criteria.where("type").is("normal"), Criteria.where("p.deleted").is(true) ); it works.

It seems to occur when you assign an existing name of a reference to a lookup result. I tried finding what causes this in the change log, but no luck.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.