0

I use the Java Spring MongoDB repository in my project.

I have this collection in MongoDB called Info:

{ "_id" : 1, "hosting" : "hostgator.com", count:7 }
{ "_id" : 2, "hosting" : "aws.amazon.com", count:7}
{ "_id" : 3, "hosting" : "aws.amazon.com", count:3}
{ "_id" : 4, "hosting" : "hostgator.com", count:5 }
{ "_id" : 5, "hosting" : "aws.amazon.com", count:1 }
{ "_id" : 6, "hosting" : "cloud.google.com", count:1 }
{ "_id" : 7, "hosting" : "aws.amazon.com", count:5 }
{ "_id" : 8, "hosting" : "hostgator.com", count:2 }
{ "_id" : 9, "hosting" : "cloud.google.com", count:3 }
{ "_id" : 10,"hosting" : "godaddy.com", count:7 }
...
{ "_id" : 100, "hosting" : "godaddy.com", count:5 }

Here is DTO definition:

public class Info{
    public int _id;
    public String hosting;
    public int count;
}

I need to write a query and to get from the database all values of count property and remove the duplications. For example, the result that I expect according to the collection above is:

List<int> counts = [1,2,3,5,7];

For this purpose I use the aggregation group method and MongoTemplate:

        GroupOperation groupOperation = Aggregation.group("count");
        Aggregation aggregation = Aggregation.newAggregation(groupOperation);
        var result = template.aggregate(aggregation, Info.class,  Info[].class);
        System.out.println(result.getMappedResults());

But the result that I get is an empty array - [].

Why I don't get the expected result?

5

0

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.