Skip to content
silviroa edited this page Apr 12, 2020 · 6 revisions

RUP

Filtrar por último estado.

Filtrar por el último estado de una prestación (ej: validada).

const pipeline = [
    {
        $match: ....
    },
    {
        $addFields: {
            lastState: { $arrayElemAt: ['$estados', -1] },
        }
    },
    {
        $match: {
            lastState: 'validada'
        }
    }
]

Consulta de Otoemisiones

db.getCollection('prestaciones').aggregate([
    {
        $match: {
            'solicitud.tipoPrestacion.conceptId': '2091000013100',
            'solicitud.organizacion.id': ObjectId("57e9670e52df311059bc8964"),
            'estados': {
                $elemMatch: {
                    'tipo': 'ejecucion',
                    'createdAt': {
                        $gt: ISODate("2018-08-01T00:27:35.658Z"),
                        $lt: ISODate("2019-05-31T23:27:35.658Z"),
                    }
                }
            }
        }
    },
    {

        $facet: {
            total: [{ $count: 'paciente' }],
            porPaciente: [
                { $group: { _id: '$paciente.id', paciente: { $first: '$paciente' } } },
                { $count: 'paciente' }

            ]
        }
    }
])

Obtener la primer prestación registrada a un paciente con determinados conceptos

    db.getCollection('prestaciones').aggregate([{ $match: { 'estados.tipo': 'validada',
        'ejecucion.registros.concepto.conceptId': { $in: [...] } } },
        { $project: { nombre: '$paciente.nombre', apellido: '$paciente.apellido',
            documento: '$paciente.documento',
            conceptId: '$ejecucion.registros.concepto.conceptId',
            term: '$ejecucion.registros.concepto.term',
            fechaPrestacion: '$ejecucion.fecha'} },
        { $sort: { fechaPrestacion: 1} },
        { $group: {
            _id: "$documento",
            prestaciones: { $push: "$$ROOT" }
        } },
        { $replaceRoot: {
            newRoot: { $arrayElemAt: ["$prestaciones", 0] }
    }}
    ])

Listado de Internaciones Estadistica

db.getCollection('prestaciones').aggregate([ 
        {
            "$match" : {
                "ejecucion.registros.valor.informeIngreso.fechaIngreso" : {
                    "$gte" : "#desde",
                    "$lte" : "#hasta"
                },
                "solicitud.ambitoOrigen" : "internacion",
                "solicitud.tipoPrestacion.conceptId" : "32485007",
                "#organizacion" : true
            }
        }, 
        {
            "$addFields" : {
                "lastState" : {
                    "$arrayElemAt" : [ 
                        "$estados", 
                        -1
                    ]
                }
            }
        }, 
        {
            "$match" : {
                "lastState" : {
                    "$ne" : "modificada"
                }
            }
        }, 
        {
            "$lookup" : {
                "from" : "paciente",
                "localField" : "paciente.id",
                "foreignField" : "_id",
                "as" : "pacienteCompleto"
            }
        }, 
        {
            "$addFields" : {
                "paciente" : {
                    "$mergeObjects" : [ 
                        {
                            "$arrayElemAt" : [ 
                                "$pacienteCompleto", 
                                0
                            ]
                        }, 
                        "$paciente"
                    ]
                }
            }
        }, 
        {
            "$addFields" : {
                "direccion" : {
                    "$arrayElemAt" : [ 
                        "$paciente.direccion", 
                        0
                    ]
                }
            }
        }, 
        {
            "$lookup" : {
                "from" : "localidad",
                "localField" : "direccion.ubicacion.localidad._id",
                "foreignField" : "_id",
                "as" : "localidad"
            }
        }, 
        {
            "$addFields" : {
                "localidad" : {
                    "$arrayElemAt" : [ 
                        "$localidad", 
                        0
                    ]
                },
                "ingreso" : {
                    "$arrayElemAt" : [ 
                        "$ejecucion.registros", 
                        0
                    ]
                },
                "egreso" : {
                    "$arrayElemAt" : [ 
                        "$ejecucion.registros", 
                        1
                    ]
                }
            }
        }, 
        {
            "$addFields" : {
                "progenitor" : {
                    "$arrayElemAt" : [ 
                        {
                            "$filter" : {
                                "input" : "$paciente.relacion",
                                "as" : "relacion",
                                "cond" : {
                                    "$gte" : [ 
                                        "$$relacion.nombre", 
                                        "progenitor/a"
                                    ]
                                }
                            }
                        }, 
                        0
                    ]
                },
                "especialidad" : {
                    "$arrayElemAt" : [ 
                        "$ingreso.valor.informeIngreso.especialidades", 
                        0
                    ]
                },
                "diagnostico_1" : {
                    "$arrayElemAt" : [ 
                        "$egreso.valor.InformeEgreso.otrosDiagnosticos", 
                        0
                    ]
                },
                "diagnostico_2" : {
                    "$arrayElemAt" : [ 
                        "$egreso.valor.InformeEgreso.otrosDiagnosticos", 
                        1
                    ]
                },
                "diagnostico_3" : {
                    "$arrayElemAt" : [ 
                        "$egreso.valor.InformeEgreso.otrosDiagnosticos", 
                        2
                    ]
                },
                "diagnostico_4" : {
                    "$arrayElemAt" : [ 
                        "$egreso.valor.InformeEgreso.otrosDiagnosticos", 
                        3
                    ]
                },
                "nacimientos_1" : {
                    "$arrayElemAt" : [ 
                        "$egreso.valor.InformeEgreso.nacimientos", 
                        0
                    ]
                },
                "nacimientos_2" : {
                    "$arrayElemAt" : [ 
                        "$egreso.valor.InformeEgreso.nacimientos", 
                        1
                    ]
                },
                "nacimientos_3" : {
                    "$arrayElemAt" : [ 
                        "$egreso.valor.InformeEgreso.nacimientos", 
                        2
                    ]
                },
                "nacimientos_4" : {
                    "$arrayElemAt" : [ 
                        "$egreso.valor.InformeEgreso.nacimientos", 
                        3
                    ]
                }
            }
        }, 
        {
            "$project" : {
                "_id" : 0,
                "PrestacionId" : "$_id",
                "AnioInfor" : {
                    "$dateToString" : {
                        "date" : {
                            "$cond" : {
                                "if" : {
                                    "$eq" : [ 
                                        {
                                            "$type" : "$egreso.valor.InformeEgreso.fechaEgreso"
                                        }, 
                                        "string"
                                    ]
                                },
                                "then" : {
                                    "$dateFromString" : {
                                        "dateString" : "$egreso.valor.InformeEgreso.fechaEgreso"
                                    }
                                },
                                "else" : "$egreso.valor.InformeEgreso.fechaEgreso"
                            }
                        },
                        "format" : "%Y"
                    }
                },
                "Estab" : "$ejecucion.organizacion.nombre",
                "CodEst" : "01400011",
                "HistClin" : "$ingreso.valor.informeIngreso.nroCarpeta",
                "Apellido" : "$paciente.apellido",
                "Nombre" : "$paciente.nombre",
                "CodDocum" : "1",
                "NumDocum" : "$paciente.documento",
                "NacDia" : {
                    "$dateToString" : {
                        "date" : "$paciente.fechaNacimiento",
                        "format" : "%d"
                    }
                },
                "NacMes" : {
                    "$dateToString" : {
                        "date" : "$paciente.fechaNacimiento",
                        "format" : "%m"
                    }
                },
                "NacAnio" : {
                    "$dateToString" : {
                        "date" : "$paciente.fechaNacimiento",
                        "format" : "%Y"
                    }
                },
                "CodUniEdad" : "1",
                "UniEdad" : "años",
                "EdadIng" : {
                    "$toInt" : {
                        "$divide" : [ 
                            {
                                "$subtract" : [ 
                                    "$ingreso.valor.informeIngreso.fechaIngreso", 
                                    {
                                        "$cond" : {
                                            "if" : {
                                                "$eq" : [ 
                                                    {
                                                        "$type" : "$paciente.fechaNacimiento"
                                                    }, 
                                                    "string"
                                                ]
                                            },
                                            "then" : {
                                                "$dateFromString" : {
                                                    "dateString" : "$paciente.fechaNacimiento"
                                                }
                                            },
                                            "else" : "$paciente.fechaNacimiento"
                                        }
                                    }
                                ]
                            }, 
                            NumberLong(31536000000)
                        ]
                    }
                },
                "CodDocumM" : "1",
                "NumDocumM" : {
                    "$ifNull" : [ 
                        "$progenitor.documento", 
                        null
                    ]
                },
                "CodLosRes" : {
                    "$substr" : [ 
                        "$localidad.codLocalidad", 
                        5, 
                        8
                    ]
                },
                "LocRes" : {
                    "$ifNull" : [ 
                        "$localidad.nombre", 
                        null
                    ]
                },
                "CodDepRes" : {
                    "$substr" : [ 
                        "$localidad.codLocalidad", 
                        2, 
                        5
                    ]
                },
                "DepRes" : {
                    "$ifNull" : [ 
                        "$localidad.departamento", 
                        null
                    ]
                },
                "CodProvRes" : {
                    "$substr" : [ 
                        "$localidad.codLocalidad", 
                        0, 
                        2
                    ]
                },
                "ProvRes" : {
                    "$ifNull" : [ 
                        "$localidad.provincia.nombre", 
                        null
                    ]
                },
                "CodPaisRes" : "200",
                "PaisRes" : "Argentina",
                "CodSexo" : "$paciente.sexo",
                "Sexo" : "$paciente.sexo",
                "CodAsoc" : {
                    "$cond" : {
                        "if" : {
                            "$ne" : [ 
                                "$ingreso.valor.informeIngreso.obraSocial", 
                                null
                            ]
                        },
                        "then" : 1,
                        "else" : 5
                    }
                },
                "Osoc" : {
                    "$ifNull" : [ 
                        "$ingreso.valor.informeIngreso.obraSocial.nombre", 
                        null
                    ]
                },
                "CodNivelInst" : {
                    "$ifNull" : [ 
                        "$ingreso.valor.informeIngreso.nivelInstruccion", 
                        null
                    ]
                },
                "NivelInst" : {
                    "$ifNull" : [ 
                        "$ingreso.valor.informeIngreso.nivelInstruccion", 
                        null
                    ]
                },
                "CodSitLab" : {
                    "$ifNull" : [ 
                        "$ingreso.valor.informeIngreso.situacionLaboral", 
                        null
                    ]
                },
                "SitLab" : {
                    "$ifNull" : [ 
                        "$ingreso.valor.informeIngreso.situacionLaboral", 
                        null
                    ]
                },
                "CodOcupac" : {
                    "$ifNull" : [ 
                        "$ingreso.valor.informeIngreso.ocupacionHabitual.codigo", 
                        null
                    ]
                },
                "Ocupac" : {
                    "$ifNull" : [ 
                        "$ingreso.valor.informeIngreso.ocupacionHabitual.nombre", 
                        null
                    ]
                },
                "CodHospPor" : {
                    "$ifNull" : [ 
                        "$ingreso.valor.informeIngreso.origen", 
                        null
                    ]
                },
                "HospPor" : {
                    "$ifNull" : [ 
                        "$ingreso.valor.informeIngreso.origen", 
                        null
                    ]
                },
                "Origen" : {
                    "$ifNull" : [ 
                        "$ingreso.valor.informeIngreso.organizacionOrigen.nombre", 
                        null
                    ]
                },
                "FecIngreso" : {
                    "$dateToString" : {
                        "date" : "$ingreso.valor.informeIngreso.fechaIngreso",
                        "format" : "%d/%m/%Y",
                        "timezone" : "America/Argentina/Buenos_Aires"
                    }
                },
                "FecEgreso" : {
                    "$dateToString" : {
                        "date" : "$egreso.valor.InformeEgreso.fechaEgreso",
                        "format" : "%d/%m/%Y",
                        "timezone" : "America/Argentina/Buenos_Aires"
                    }
                },
                "EspecEgre" : {
                    "$ifNull" : [ 
                        "$especialidad.term", 
                        null
                    ]
                },
                "CodEspecEgre" : {
                    "$ifNull" : [ 
                        "$especialidad.conceptId", 
                        null
                    ]
                },
                "ServEgre" : {
                    "$ifNull" : [ 
                        "$especialidad.conceptId", 
                        null
                    ]
                },
                "CodServEgre" : {
                    "$ifNull" : [ 
                        "$especialidad.conceptId", 
                        null
                    ]
                },
                "DiasTotEst" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.diasDeEstada", 
                        null
                    ]
                },
                "CodEgresP" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.EgresP", 
                        null
                    ]
                },
                "Lugar_Trasl" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.UnidadOrganizativaDestino.nombre", 
                        null
                    ]
                },
                "CodDiagPr" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.diagnosticoPrincipal.codigo", 
                        null
                    ]
                },
                "OtDiag1" : {
                    "$ifNull" : [ 
                        "$diagnostico_1.codigo", 
                        null
                    ]
                },
                "OtDiag2" : {
                    "$ifNull" : [ 
                        "$diagnostico_2.codigo", 
                        null
                    ]
                },
                "OtDiag3" : {
                    "$ifNull" : [ 
                        "$diagnostico_3.codigo", 
                        null
                    ]
                },
                "OtDiag4" : {
                    "$ifNull" : [ 
                        "$diagnostico_4.codigo", 
                        null
                    ]
                },
                "CodCauExtT" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.causaExterna.producidaPor.nombre", 
                        null
                    ]
                },
                "CauExtT" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.causaExterna.producidaPor.id", 
                        null
                    ]
                },
                "CodCauExtL" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.causaExterna.lugar.nombre", 
                        null
                    ]
                },
                "CauExtL" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.causaExterna.lugar.nombre", 
                        null
                    ]
                },
                "CodCauExt" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.causaExterna.comoSeProdujo.codigo", 
                        null
                    ]
                },
                "FecTermEmb" : {
                    "$dateToString" : {
                        "date" : "$egreso.valor.InformeEgreso.terminacionEmbarazo",
                        "format" : "%d/%m/%Y",
                        "timezone" : "America/Argentina/Buenos_Aires"
                    }
                },
                "EdadGestac" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.edadGestacional", 
                        null
                    ]
                },
                "Paridad" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.paridad", 
                        null
                    ]
                },
                "CodTipPart" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.tipoParto", 
                        null
                    ]
                },
                "TipPart" : {
                    "$ifNull" : [ 
                        "$egreso.valor.InformeEgreso.tipoParto", 
                        null
                    ]
                },
                "PesoNacerRN_1" : {
                    "$ifNull" : [ 
                        "$nacimientos_1.pesoAlNacer", 
                        null
                    ]
                },
                "CodCondNacRN_1" : {
                    "$ifNull" : [ 
                        "$nacimientos_1.condicionAlNacer", 
                        null
                    ]
                },
                "CondNacRN_1" : {
                    "$ifNull" : [ 
                        "$nacimientos_1.condicionAlNacer", 
                        null
                    ]
                },
                "CodTermRN_1" : {
                    "$ifNull" : [ 
                        "$nacimientos_1.terminacion", 
                        null
                    ]
                },
                "TermRN_1" : {
                    "$ifNull" : [ 
                        "$nacimientos_1.terminacion", 
                        null
                    ]
                },
                "SexoRN_1" : {
                    "$ifNull" : [ 
                        "$nacimientos_1.sexo", 
                        null
                    ]
                },
                "CodSexoRN_1" : {
                    "$ifNull" : [ 
                        "$nacimientos_1.sexo", 
                        null
                    ]
                },
                "PesoNacerRN_2" : {
                    "$ifNull" : [ 
                        "$nacimientos_2.pesoAlNacer", 
                        null
                    ]
                },
                "CodCondNacRN_2" : {
                    "$ifNull" : [ 
                        "$nacimientos_2.condicionAlNacer", 
                        null
                    ]
                },
                "CondNacRN_2" : {
                    "$ifNull" : [ 
                        "$nacimientos_2.condicionAlNacer", 
                        null
                    ]
                },
                "CodTermRN_2" : {
                    "$ifNull" : [ 
                        "$nacimientos_2.terminacion", 
                        null
                    ]
                },
                "TermRN_2" : {
                    "$ifNull" : [ 
                        "$nacimientos_2.terminacion", 
                        null
                    ]
                },
                "SexoRN_2" : {
                    "$ifNull" : [ 
                        "$nacimientos_2.sexo", 
                        null
                    ]
                },
                "CodSexoRN_2" : {
                    "$ifNull" : [ 
                        "$nacimientos_2.sexo", 
                        null
                    ]
                },
                "PesoNacerRN_3" : {
                    "$ifNull" : [ 
                        "$nacimientos_3.pesoAlNacer", 
                        null
                    ]
                },
                "CodCondNacRN_3" : {
                    "$ifNull" : [ 
                        "$nacimientos_3.condicionAlNacer", 
                        null
                    ]
                },
                "CondNacRN_3" : {
                    "$ifNull" : [ 
                        "$nacimientos_3.condicionAlNacer", 
                        null
                    ]
                },
                "CodTermRN_3" : {
                    "$ifNull" : [ 
                        "$nacimientos_3.terminacion", 
                        null
                    ]
                },
                "TermRN_3" : {
                    "$ifNull" : [ 
                        "$nacimientos_3.terminacion", 
                        null
                    ]
                },
                "SexoRN_3" : {
                    "$ifNull" : [ 
                        "$nacimientos_3.sexo", 
                        null
                    ]
                },
                "CodSexoRN_3" : {
                    "$ifNull" : [ 
                        "$nacimientos_3.sexo", 
                        null
                    ]
                },
                "PesoNacerRN_4" : {
                    "$ifNull" : [ 
                        "$nacimientos_4.pesoAlNacer", 
                        null
                    ]
                },
                "CodCondNacRN_4" : {
                    "$ifNull" : [ 
                        "$nacimientos_4.condicionAlNacer", 
                        null
                    ]
                },
                "CondNacRN_4" : {
                    "$ifNull" : [ 
                        "$nacimientos_4.condicionAlNacer", 
                        null
                    ]
                },
                "CodTermRN_4" : {
                    "$ifNull" : [ 
                        "$nacimientos_4.terminacion", 
                        null
                    ]
                },
                "TermRN_4" : {
                    "$ifNull" : [ 
                        "$nacimientos_4.terminacion", 
                        null
                    ]
                },
                "SexoRN_4" : {
                    "$ifNull" : [ 
                        "$nacimientos_4.sexo", 
                        null
                    ]
                },
                "CodSexoRN_4" : {
                    "$ifNull" : [ 
                        "$nacimientos_4.sexo", 
                        null
                    ]
                }
            }
        }
    ])
Clone this wiki locally