How to aggregate in mongodb via a Repository in javascript/Node?

Asked

Viewed 20 times

1

I have a repository that makes a query in the collection logsMonitrip. The method below is called by the service when the user tries to make a query through the company’s cnpj, after that I get the xId of the collection and try to make an aggregation between the collection errosand logsMonitrip, where both of them Xids are equal, to then return the data I need.

I hope it’s just a syntax error.

let criteria = {};

    if(idLog){
        criteria["idLog"] = idLog;
    }

    if(cnpjEmpresaTransporte){
        criteria["cnpjEmpresaTransporte"] = cnpjEmpresaTransporte;
    }

    criteria["idCliente"] = clienteId;        

    criteria["$and"] = [{"dataHoraEvento": {$gte: dataIni}}, {"dataHoraEvento": {$lte: dataFim}}];

    criteria["$lookup"] = [{from: "erros",
    localField: "xId",
    foreignField: "xId",        
    as: "Logs" }]

    console.info(criteria);
    this._logger.info(`LogRepository - obterLogsCnpj - idCliente: ${clienteId} - idLog: ${idLog} - cnpjEmpresaTransporte: ${cnpjEmpresaTransporte} - dataIni: ${dataIni} - dataFim: ${dataFim}`);

        return this._prepareResult(criteria);
}

_prepareResult(criteria) {
    return this._log.aggregate(criteria, {"_class": 0, "_id": 0}).lean().exec();    
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.