Description
Hi I have following pojos :
package model;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import java.util.List;
@Document(collection = "pojo")
@Data
public class Pojo {
@Id
String id;
List<PojoLine> lines;
@Field("pojo_header")
PojoHeader header;
Integer total;
}
package model;
import lombok.Data;
@Data
public class PojoHeader {
String name;
Integer amount;
}
package model;
import lombok.Data;
@Data
public class PojoLine {
String status;
String description;
}
Document in my db is :
{
"_id" : "1",
"lines" : [
{
"status" : "Created",
"description" : "Create"
},
{
"status" : "Deleted",
"description" : "Delete"
}
],
"pojo_header" : {
"name" : "Test",
"amount" : NumberInt(1)
},
"total" : NumberInt(1)
}
My aggregation query is :
List<AggregationOperation> pipeline = Arrays.asList(
Aggregation.match(new Criteria("lines.status").is("Deleted")
.and("header.name").is("Test")),
Aggregation.unwind("$lines"),
Aggregation.match(new Criteria("lines.status").is("Deleted")
.and("header.name").is("Test"))
);
TypedAggregation<Pojo> aggregation = Aggregation.newAggregation(Pojo.class, pipeline);
Ideally, I should get back one document. But I don't get any output.
Upon debugging I found the aggregation query internally is :
[{
"$match": {
"lines.status": "Delete",
"pojo_header.name": "Test"
}
}, {
"$unwind": "$lines"
}, {
"$match": {
"lines.status": "Delete",
"header.name": "Test"
}
}]
Here in the first match stage, I have the correct fieldName as per @field annotation i.e. pojo_header.name
but in the stage after unwind it's header.name instead of pojo_header.name
Test code to showcase issue is present in: https://github.com/scrum-futch/Aggregation-issue
Test code that was previous working before updating here: https://github.com/scrum-futch/Aggregation-issue/tree/Previously_working
aggregation pipeline is logged after log "----Check the pipeline log after this-----"
Please share if there is any workaround for the same.
MongoDB: 4.4