I have 2 collections (articles & tags). Articles has 'tags' which is array of objectIds. I am trying to join articles and tags so as to get tagName from tags. Below is my mongodb query:
db.articles.aggregate([
{"$unwind": "$tags"},
{"$lookup": {
"localField": "tags",
"from": "tags",
"foreignField": "_id",
"as": "materialTags"
}
}
])
I converted this into spring Data as below
UnwindOperation unwindOperation = Aggregation.unwind("tags");
LookupOperation lookupOperation1 = LookupOperation.newLookup()
.from("tags")
.localField("tags")
.foreignField("_id")
.as("materialTags");
Aggregation aggregation = Aggregation.newAggregation(unwindOperation, lookupOperation1 );
}
AggregationResults<Article> resultList
= mongoTemplate.aggregate(aggregation, "articles", Article.class);
My entity Article.class
....
private List<ObjectId> tags = new ArrayList<>(); //array of tag ids
@Transient
private List<Tag> materialTags = new ArrayList<>(); // array of all tag details
//getters and setters
...
Tag.class
@Id private ObjectId id;
private String tagName;
...
If I use mongodb query, I am getting 'materialTags' array list with tag object filled in. Through Spring Data, I do get the exact same result. After unwind I get multiple rows (1 row per tag array entry) which is correct. Through debug point I can see resultList -> rawResult contain 'materialTags'. But resultList -> mappedResults -> 'materialTags' is empty.
Why Spring Data does not give me proper result inside Mapped Result? What am I doing wrong ? Please help.
Thanks in advance.