1

I have articles & tags collection. Articles contain tags which is array of objectId. I want to fetch tagName as well, so I unwind (this gives me multiple rows - 1 per tag array entry) => lookup (joins with tabs collection) => group (combine it into original result set)

My mongodb query is as follows, which gives me correct result:

db.articles.aggregate([
  {"$unwind": "$tags"},
  {
    "$lookup": {
      "localField": "tags",
      "from": "tags",
      "foreignField": "_id",
      "as": "materialTags"
    }
 },
 {
    "$group": {
      "_id": "$_id",
      "title": {"$first": "$title"},
      "materialTags": {"$push": "$materialTags"}
    }
  }
])

My corresponding Spring code:

UnwindOperation unwindOperation = Aggregation.unwind("tags");
LookupOperation lookupOperation1 = LookupOperation.newLookup()
    .from("tags")
    .localField("tags")
    .foreignField("_id")
    .as("materialTags");

//I also want to add group operation but unable to find the proper syntax ??. 
Aggregation aggregation = Aggregation.newAggregation(unwindOperation, 
 lookupOperation1, ??groupOperation?? );
AggregationResults<Article> resultList
    = mongoTemplate.aggregate(aggregation, "articles", Article.class);

I tried to play around with group operation but without much luck. How can I add group operations as per original query ?

Thanks in advance.

user2869612
  • 607
  • 2
  • 10
  • 32

1 Answers1

2

Group query syntax in Spring for

{
    "$group": {
      "_id": "$_id",
      "title": {"$first": "$title"},
      "materialTags": {"$push": "$materialTags"}
    }
}

is

Aggregation.group("_id").first("title").as("title").push("materialTags").as("materialTags")

Final query

UnwindOperation unwindOperation = Aggregation.unwind("tags");
LookupOperation lookupOperation1 = LookupOperation.newLookup()
    .from("tags")
    .localField("tags")
    .foreignField("_id")
    .as("materialTags");

Aggregation aggregation = Aggregation.newAggregation(unwindOperation, 
 lookupOperation1, Aggregation.group("_id").first("title").as("title").push("materialTags").as("materialTags") );
AggregationResults<Article> resultList
    = mongoTemplate.aggregate(aggregation, "articles", Article.class);

To get more info please go thru the below references

http://www.baeldung.com/spring-data-mongodb-projections-aggregations

spring data mongodb group by

Create Spring Data Aggregation from MongoDb aggregation query

https://www.javacodegeeks.com/2016/04/data-aggregation-spring-data-mongodb-spring-boot.html

Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34