[英]How to write this aggregation query on Mongodb (Spring)
我嘗試了很多方法,但是我不知道如何使它在Spring MVC上起作用:/,我將查詢和Json對象附加到MongoDB上,我在沒有任何幫助的情況下閱讀了很多網頁,我不知道如何要做到這一點,請幫助我。
MongoDB:
db.denuncia.aggregate([
{ $project: { "estadosDenuncia": { $arrayElemAt: ['$estadosDenuncia', -1] } } },
{ $match: { 'estadosDenuncia.estado': 'Proceso' } },
{
$count: "Proceso"
}
]);
傑森:
{
"_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"
}
親切的問候。
亞歷杭德羅·B。
我自己找到了解決方案,希望對某人有用。
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.