Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda