0

Hi a am try to do rest api in spring boot with mongodb to find group by count the input data look like. please share any logic, code, example link. guys i am expecting spring boot logic. how mongodb aggregation framework integrating.

{
    "_id" : "PRODUCT_01",
    "productname" : "product1",
    "value" : "codesoft"
},
{
    "_id" : "PRODUCT_01",
    "productname" : "product2",
    "value" : "codesoft"
},

{
    "_id" : "PRODUCT_01",
    "productname" : "product1",
    "value" : "codesoft"
}

expected output { product1 : 2, product2 : 1 } Any help is appreciated.

3
  • Did you try on your own ? where is your code? Commented Aug 27, 2018 at 5:54
  • Possible duplicate of MongoDB SELECT COUNT GROUP BY Commented Aug 27, 2018 at 5:55
  • Amit i have create but it is not proper solution that's why i have not share.. Commented Aug 27, 2018 at 10:06

2 Answers 2

4

try this

 db.testColln.aggregate(
{ 
$group : {_id : "$productname", total : { $sum : 1 }}
}
 );

for Spring Boot

Aggregation agg = newAggregation(
        group("productname").count().as("total")
        project("productname").and("total"),

    );
AggregationResults<Product> groupResults 
        = mongoTemplate.aggregate(agg, Product.class,Result.class);
    List<Result> result = groupResults.getMappedResults();

 public class Result {

     private String productname;

      private long total;

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

2 Comments

Can you explain your solution? Which parts are relevant, and why do you think they solve the problem?
gaurav thanx for this , but i required spring boot mongo aggregation logic not mongo query .. mongo i know.
0
@GetMapping("/group")
public List<ProductCount> groupByName() {

// grouping by prductName
  GroupOperation groupOperation = 
         Aggregation.group("productName").count().as("count");
// projection operation
 ProjectionOperation projectionOperation = 
 Aggregation.project("count").and("productName").previousOperation();      
// sorting in ascending
 SortOperation sortOperation = 
        Aggregation.sort(Sort.by(Sort.Direction.ASC, "count"));
// aggregating all 3 operations using newAggregation() function
 Aggregation aggregation = 
        Aggregation.newAggregation(groupOperation,projectionOperation 
        ,sortOperation);
// putting in a list 
// "products" is collection name
 AggregationResults<ProductCount> result =
        mongotemplate.aggregate(aggregation, "products", 
        ProductCount.class);    
 return result.getMappedResults();
    }

$ make ProductCount class in model package

public class ProductCount {
  private String productName;
  private int count;

  #getters
  #setters

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.