简体   繁体   中英

How to write this aggregation query on Mongodb (Spring)

I tried a lot of method but i don't know how to make it works on Spring MVC :/, I attach my query and my Json objects on MongoDB, i read a lot of webpages without any help, I don't understand how to make it, please help me.

MongoDB :

db.denuncia.aggregate([
  { $project: { "estadosDenuncia": { $arrayElemAt: ['$estadosDenuncia', -1] } } },
  { $match: { 'estadosDenuncia.estado': 'Proceso' } },
    {
      $count: "Proceso"
    }
]);

Json :

{
    "_id" : ObjectId("5c1073b76c22a6052254bf79"),
    "nombreColegio" : "LICEO POLITECNICO PARTICULAR HANNOVER",
    "codigo" : "gTMwIjMERHcyMlMElTMwBnNzJjVuRzQxMjUyMjTyckb4QSRPl0T=",
    "rut" : 17836217,
    "dv" : "8",
    "fechaCreacion" : ISODate("2018-12-12T02:34:31.170Z"),
    "nombres" : "Alejandro",
    "apellidopate" : "Burgos",
    "apellidomate" : "Cartagena",
    "correo" : "alejandro.burgos.c@gmail.com",
    "telefono1" : "993494291",
    "nivelCurso" : "4 Medio",
    "letraCurso" : "E",
    "detalleDenuncia" : "PruebaSADDSADASAD  AD DK DS DAKK DSDS .",
    "rbdColegio" : 9897,
    "respuestas" : [ 
        {
            "sospechas" : null,
            "pregunta15" : null,
            "pregunta16" : null,
            "pregunta13" : null,
            "pregunta14" : null,
            "datos" : null,
            "pregunta1" : "a",
            "pregunta11" : null,
            "pregunta12" : null,
            "pregunta10" : null,
            "pregunta5" : null,
            "pregunta4" : null,
            "detalles" : null,
            "pregunta3" : "a",
            "pregunta2" : "a",
            "pregunta9" : null,
            "pregunta8" : null,
            "pregunta7" : null,
            "pregunta6" : null
        }
    ],
    "estadosDenuncia" : [ 
        {
            "estado" : "Pendiente",
            "fecha" : ISODate("2018-12-12T02:34:31.157Z"),
            "usuario" : "5b7ce5178eb2c00970bfa05b",
            "comentario" : "Ingreso de denuncia al sistema."
        }, 
        {
            "estado" : "Proceso",
            "fecha" : ISODate("2018-12-15T00:00:00.157Z"),
            "usuario" : "5b7ce5178eb2c00970bfa05b",
            "comentario" : "Denuncia en revisión."
        }
    ],
    "_class" : "cl.adatos.application.convesapp.models.Denuncia"
}

Kind Regards.

Alejandro B.

I found the solution on my own, I hope that for someone it can be useful.

public String totalDenunciasEstadisticas() {
    ProjectionOperation proj = project().and("estadosDenuncia").arrayElementAt(-1).as("last");
    GroupOperation contar = Aggregation.group("last.estado").count().as("cantidad");

    Aggregation aggregate = Aggregation.newAggregation(proj, contar);

    AggregationResults<Denuncia> orderAggregate = mongoTemplate.aggregate(aggregate,
            "denuncia", Denuncia.class);

    return orderAggregate.getRawResults().toJson();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM