PHP execute and MongoCode alternatives

Looking for an alternative way to implement the following php code as MongoCode and $db->execute have been deprecated.

$updateDocQuery = new MongoCode ('var bulkOp = db.bsd_CampaignDetailStats.initializeOrderedBulkOp();
            db.bsd_CampaignDetailStats.find({\'CampDetail.Steps.StepID\': ' . $stepID . '}).forEach(function(doc) {
                for(var i=0; i < doc.CampDetail.length; i++) {
                  if (doc.CampDetail[i].ActionType == \'Offer\') {
                    for(var j=0; j < doc.CampDetail[i].Steps.length; j++) {
                        var updateDocument = {};
                            updateDocument[\'CampDetail.\' + i + \'.Steps.\' + j + \'.RedeemedCount\'] = ' . $doc['value']['count'] . ';      
                            updateDocument[\'CampDetail.\' + i + \'.Steps.\' + j + \'.RedeemedAmount\'] = ' . $doc['value']['total'] . ';      
                            bulkOp.find({\'_id\': doc._id}).update({
                                \'$set\': updateDocument 
                            });
                            bulkOp.execute();        
                     }
                  }
                } 
              })');
            $db->execute($updateDocQuery);

Just rewrite it as a PHP function. Nothing you are doing there is exotic.
What you’re basically doing is writing a program in Javascript and embedding in a PHP program.
Just rewrite your little program in PHP.

1 Like