0

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.

user2869612
  • 607
  • 2
  • 10
  • 32
  • Nope, this is NOT the JPA API, or even Spring Data JPA. It is Spring MongoDB ... which does not use the JPA API internally. It uses the MongoDB API –  Oct 23 '17 at 09:40
  • ok, accepted the edit. thanks for the same. – user2869612 Oct 23 '17 at 10:05

0 Answers0