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.

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.