1

I want to apply group by only on one filed with Spring Boot and MongoDB. Let say I've one collection in Mongo which consist following fields

{
  "_id": {
    "$binary": {
      "base64": "RZX+2GWyTD2DWyD/01ZPmA==",
      "subType": "04"
    }
  },
  "categoryType": "CRICKET",
  "description": "I like Cricket",
}

{
  "_id": {
    "$binary": {
      "base64": "RZX+2GWyTD2DWyD/01ZPmA==",
      "subType": "04"
    }
  },
  "categoryType": "FOOTBALL",
  "description": "I love Footbal",
}

{
  "_id": {
    "$binary": {
      "base64": "RZX+2GWyTD2DWyD/01ZPmA==",
      "subType": "04"
    }
  },
  "categoryType": "CRICKET",
  "description": "I love Cricket",
}

{
  "_id": {
    "$binary": {
      "base64": "RZX+2GWyTD2DWyD/01ZPmA==",
      "subType": "04"
    }
  },
  "categoryType": "Martial Art",
  "description": "Martial Art",
}

{
  "_id": {
    "$binary": {
      "base64": "RZX+2GWyTD2DWyD/01ZPmA==",
      "subType": "04"
    }
  },
  "categoryType": "FOOTBALL",
  "description": "Football",
}

My purpose is to display records by their group. So how I can apply group by with Spring Boot and MongoDB?

  • Does this help: https://stackoverflow.com/questions/42814325/spring-data-mongodb-group-by – Joe Dec 24 '22 at 09:57
  • Thank you @Joe. I've used it but it gives an error like: "Failed to convert from type [java.lang.String] to type [java.util.UUID] for value 'CATEGORY_NAME'; nested exception is java.lang.IllegalArgumentException: Invalid UUID string: CATEGORY_NAME". And I've tried out to sort it out but my efforts did not work!! And yes if you have any further guidance regarding this error please tell me. Thank you again – Milan Bhatt Dec 24 '22 at 19:36
  • Hi @Joe I've sorted out the above error and getting results as below: [{"_id": "Martial Art", "categoryType": [{"categoryTypeCategoryType": "Martial Art", "description": "Martial Art"}]}, {"_id": "FOOTBALL", "categoryType": [{"categoryTypeCategoryType": "FOOTBALL", "description": "FOOTBALL"}, {"categoryTypeCategoryType": "FOOTBALL", "description": "I love Footbal"}]}, {"_id": "CRICKET", "categoryType": [{"categoryTypeCategoryType": "CRICKET", "description": "I like Cricket"}, {"categoryTypeCategoryType": "CRICKET", "description": "I love Cricket"}]}] Please read the next comment – Milan Bhatt Dec 25 '22 at 06:30
  • Here I'm getting all category type records under the single _id. e.g "_id":"CRICKET". If you observed that id, getting all records which are belongs to Cricket category. But I want single record. If you can help me in this it would be great. – Milan Bhatt Dec 25 '22 at 06:33
  • I'm not sure what output you trying to get, can you edit the question to add a sample what the output should look like? – Joe Dec 25 '22 at 18:17
  • If this is a different problem than the original question, asking a new question would be appropriate. – Joe Dec 25 '22 at 18:24

0 Answers0