Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

624
Vistas
How to make a custom sorting query in spring boot for a mongo db repository?

I want to put this query with @Query annotation in my repository.

This is the query:

`db.report.find({'company' : 'Random'}).sort( { 'reportDate' : -1} ).limit(1)`

Which is the best way to implement custom queries with @Query annotations or to use MongoTemplate ?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Using Mongo Template.

Criteria find = Criteria.where("company").is("Random");
Query query = new Query().addCriteria(find).with(new Sort(Sort.Direction.DESC, "reportDate"));
BasicDBObject result = mongoOperations.findOne(query, BasicDBObject.class, "collection_name");

Using Mongo Repository

Report findTopByCompanyOrderByReportDateDesc(String company)
over 4 years ago · Santiago Trujillo Denunciar

0

Note that in the new version of Springboot(v2.2.5), the correct method is sort.by(). like this:

Query query = new Query().addCriteria(find).with(Sort.by(Sort.Direction.DESC, "reportDate"));
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda