Aggregation in mongoDB is not working right

0

Hello

I'm trying to filter some data to render a table in a view here. The template is EJS.

What I need to do is: Count how many times an id number appears with a given status.

I've been able to count the amount that an id appears, but with the status attached it does not work.

Here is an example of the code and template if you need to:

 const atendData = atendimentos.aggregate([
            { '$group' : {
                    '_id': "$id_atendente",
                    //A princípio errado
                    'Idcount': { '$sum': 1 }
                } },
            { '$sort': { '_id': 1 } }
        ]).exec();

        const testeEa = atendimentos.aggregate([
            { '$group': {
                    '_id': null,
                    //Certo
                    'eatest': {
                        '$sum': {
                            '$cond' : [ { '$eq': ['$status', 'EA'] },  1, 0]
                        }
                    }
                } }
        ]).exec();

The eatest is returning the correct number, but I am not able to divide this by id, for example idtal: x times with status 'EA'.

MODEL

module.exports = mongoose.model('atendimento', {
    id: String,
    id_atendimento: { type: Number, default: 0 },
    id_cliente: { type: Number, default: 0 },
    id_user: mongoose.Schema.Types.ObjectId,
    user_nome: String,
    cliente_nome: String,
    id_atendente: { type: Number, default: 0 },
    atendente_nome: String,
    atendente_imagem: String,
    setor: Number,
    descricao: String,
    status: String,
    date: { type: Date, default: Date.now },
    inicio: { type: Date, default: Date.now },
    fim: { type: Date, default: Date.now },
    update: { type: Date, default: Date.now }
});

I'm using a promise to send data to the front end, it's currently like this:

Promise.all([counts, monthly, testeAt, atendData, testeEa]).then(([counts, monthly, testeAt, atendData, testeEa]) => {
            const statusData = counts[0];
            const monthlyData = monthly[0];
            const atData = testeAt[0];
            const dataAtend = atendData[0];
            const dataEa = testeEa[0];

The first 3 work well and render what I need, I'm just having problems in the second part.

I would like to use $ facet to do in a single query, but the mongo version here is 3.2 and does not support it.

Thank you to anyone and try to help me!

    
asked by anonymous 10.08.2018 / 20:41

0 answers