1

In my Spring Boot application, I am querying a collection of documents. I'm trying to perform an aggregate operation, to group similar fields together and count them.

The issue is that while the right values are being returned, they should be just normal strings "[ \"Test Name\"]" should be "Test Name"

I can solve this issue for one of the fields by using the @Id annotation but spring data Mongo does not like composite keys so I can not use that as a solution.

How can I return just the string value and not the values that are given below.

{
    "exampleDateTime": 1392029442389,
    "exampleName": "[ \"Test Name\"]",
    "exampleDescription": "[ \"Test Description\"]",
    "exampleCount": 1
}

So I have a java object like this

@Document(collection = "ExampleCollection")
public class Example{
    private Date exampleDateTime;
    private String exampleName;
    private String exampleDescription;
    private int exampleCount;

    ...getters and setters...
}

The Repository Implementation with Aggregation

    public class ExampleRepositoryImpl implements ExampleRepositoryCustom {


     @Autowired
     MongoTemplate mongoTemplate;

    @Override
    public List<Example> countExampleByName() {
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.group("exampleName").addToSet("exampleName").as("exampleName").addToSet("exampleDateTime").as("exampleDateTime")
                .addToSet("exampleDescription").as("exampleDescription").count().as("exampleCount")

        AggregationResults<Example> groupResults = mongoTemplate.aggregate(
                aggregation,"ExampleCollection", Example.class);

        List<Example> result = groupResults.getMappedResults();
        return result;
    }
}
3
  • 1
    Replace your aggregation with Aggregation aggregation = Aggregation.newAggregation( Aggregation.group("exampleName").first("exampleName").as("exampleName").first("exampleDateTime").as("exampleDateTime") .first("exampleDescription").as("exampleDescription").count().as("exampleCount") . When you use addToSet, which returns collection type, spring calls toString() to convert it to string type( note the quotes around []) as defined in your pojo or Change your types in pojo to String[] if you need to use addToSet. Commented Jul 6, 2017 at 12:38
  • Ah thank you. That fixed the problem perfectly. Commented Jul 6, 2017 at 12:44
  • Np. Adding this as answer. I have seen other places where people are blindsided when spring maps the list of string values into toString() value. Commented Jul 6, 2017 at 13:56

1 Answer 1

2

When you use addToSet, which returns collection type, spring calls toString() to convert it to string type ( note the quotes around [] ) as defined in your pojo

Replace your aggregation to use first with

Aggregation aggregation = Aggregation.newAggregation( 
        Aggregation.group("exampleName")
           .first("exampleName").as("ex‌​ampleName")
           .first("e‌​xampleDateTime").as(‌​"exampleDateTime") 
           .first("exampleDescription").as("exampleDescription")
           .count(‌​).as("exampleCount")

OR

Change your types in pojo to String[] or collection of string if you need to use addToSet

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.