Error - can't convert undefined to object : DBCollection.prototype.findOneAndUpdate

I have been with this problem for two days, I want to see if you can help me, the problem is this error.

when I try to do this query.

db.getCollection('inversionista').find({broker: id}).forEach(function(scope) { 
    db.getCollection('puja').find({inversionista: scope._id}).forEach(function(scope) { 
        const subasta = db.getCollection('subasta').findOne({_id: scope.subasta})
        const factura = db.getCollection('factura').findOne({_id: subasta.factura})
        const pyme = db.getCollection('pyme').findOne({_id: factura.proveedor})
        const inversionista = db.getCollection('inversionista').findOne({_id: scope.inversionista})
        const dias = factura.dias
        var percetange = 0;
        var item;
        var monto;
        if(pyme.broker && pyme.broker == id){
              if (dias <= 45) {
                percetange = 0.003;
              } else if (dias > 45 && dias <= 75) {
                percetange = 0.004;
              } else if (dias > 75) {
                percetange = 0.005;
              }
              monto = factura.montoSubastar;
              item = {
                valueId: pyme.razon_social,
                tipoId: 'FACTURA',
                subastaId: scope.subasta,
                moneda: factura.moneda,
                fechaRegistro: scope.informacionPagoGarantia.fechaPagoRealCavaliBLP,
                monto: percetange * monto
              }
              db.getCollection('broker').findOneAndUpdate({
                 _id: inversionista.broker,
                 $push: { transacciones: {$each: [item] } } ,
              })
        }
        if (dias <= 45) {
            percetange = 0.0035;
        } else if (dias > 45 && dias <= 75) {
            percetange = 0.0065;
        } else if (dias > 75) {
            percetange = 0.0085;
        }
        monto = factura.monto_adjudicado;
        item = {
            valueId: inversionista.nombres + ' ' + inversionista.apellidos,
            subastaId: scope.subasta,
            tipoId: 'INVERSIONISTA',
            moneda: factura.moneda,
            fechaRegistro: scope.informacionPagoGarantia.fechaPagoRealCavaliBLP,
            monto: percetange * monto
        };
        db.getCollection('broker').findOneAndUpdate({
            _id: inversionista.broker,
            $push: { transacciones: { $each: [item] }  },
        });
    });
});

error that I have that this.

can’t convert undefined to object :
DBCollection.prototype.findOneAndUpdate

The way your code is presented is very hard to understand. Please take a look at following and edit your post accordingly. It will help us help you better.

Thanks for formatting the code.

Since you have 2 calls to findOneAndUpdate(), I would start by identify which one is failing and for which data. May be the full stack trace indicates which of the call is failing. Catching the error and printing the current data will help you pinpoint the problem.

One thing I not is that you might be using the wrong syntax. If you look at the examples given in https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/, you will see that it is …findOneAndUpdate( query , update ). Your $push is part of the first parameter so it is part of the query. You then end up with no update parameter, most likely the undefined. I will try the following call instead.

db.getCollection('broker').findOneAndUpdate({ _id: inversionista.broker } ,
                 { $push: { transacciones: {$each: [item] } } })

I do not think you need { $each : [ item ] }. Just item should work.

Lastly, I will look at the aggregation framework, in particular, $lookup and $graphLookup because you are making 4 database access within 2 loops that are also making database access. I pretty sure you can get your data with a single aggregation pipeline.