PHP code example of thesebas / php-mongodb-helpers

1. Go to this page and download the library: Download thesebas/php-mongodb-helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thesebas / php-mongodb-helpers example snippets


$collection->aggregate([

  project([
      'field' => reduce(
          filter(
              path(...$arrayField),
              'tmp',
              eq(variable("tmp", ...$filterField), $filterValue)
          ),
          0,
          add(variable('value'), variable("this", $sumField))
      )
  ])

]);

$collection->aggregate([
  ['$project' => [
      'field' => ['$reduce' => [
          'input' => ['$filter' => [
              'input' => '$' . join('.', $arrayField),
              'as' => 'tmp',
              'cond' => ['$eq' => ["\$\$tmp." . join('.', $filterField), $filterValue]]
          ]],
          'initialValue' => 0,
          'in' => [
              '$add' => ['$$value', '$$this.' . $sumField]
          ]
      ]]
  ]]
]);