Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Obtain java primitive from mongo aggregation without a new output class

I have an aggregation:

AggregationResults<Integer> result = mongoTemplate.aggregate(
  Aggregation.newAggregation(
    Aggregation.group().count().as("value"), 
    Aggregation.project("value").andExclude("_id"), 
  MyData.class, Integer.class);

In the mongo shell, when I don't have to map an object, I get: { "value" : 2 }

However, I get the following error when trying to map this lone value: org.springframework.data.mapping.model.MappingException: No mapping metadata found for java.lang.Integer

Can I get around having to create a new output type class, when I only want to get a single java primitive?

Note: I'm going this approach instead of db.collection.count() for the sharding inaccuracies stated here - https://docs.mongodb.com/manual/reference/method/db.collection.count/#sharded-clusters

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

AggregationResults<DBObject> result = mongoTemplate.aggregate(
  Aggregation.newAggregation(
    Aggregation.group().count().as("value"), 
    Aggregation.project("value").andExclude("_id"), 
MyData.class, DBObject.class);

int count = (Integer) result.getUniqueMappedResult().get("value");

So, not exactly what I wanted, because I still have to traverse over an object, but it's not any more code than I had before and I didn't need to make another class as the outputType.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!