PHP code example of nddcoder / sql-to-mongodb-query

1. Go to this page and download the library: Download nddcoder/sql-to-mongodb-query 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/ */

    

nddcoder / sql-to-mongodb-query example snippets


$parser = new Nddcoder\SqlToMongodbQuery\SqlToMongodbQuery();
$query = $parser->parse("
    SELECT id, username, email, created_at 
    FROM users
    USE INDEX active_1_created_at_1
    WHERE active = true and created_at >= date('2021-01-01') 
    ORDER BY created_at desc 
    LIMIT 10, 20
");

/*
Nddcoder\SqlToMongodbQuery\Model\FindQuery {#473
  +filter: array:2 [
    "active" => true
    "created_at" => array:1 [
      "$gte" => MongoDB\BSON\UTCDateTime {#926
        +"milliseconds": "1609459200000"
      }
    ]
  ]
  +projection: array:4 [
    "id" => 1
    "username" => 1
    "email" => 1
    "created_at" => 1
  ]
  +sort: array:1 [
    "created_at" => -1
  ]
  +limit: 20
  +skip: 10
  +collection: "users"
  +hint: "active_1_created_at_1"
}

*/

$parser = new Nddcoder\SqlToMongodbQuery\SqlToMongodbQuery();
$query = $parser->parse("
    SELECT date, count(*)
    FROM clicks
    USE INDEX status_1_created_at_1
    WHERE status = 1 and created_at >= date('2021-07-01') 
    GROUP BY date
    HAVING count(*) > 100
");

/*
Nddcoder\SqlToMongodbQuery\Model\Aggregate {#493
  +pipelines: array:4 [
    0 => array:1 [
      "$match" => array:2 [
        "status" => 1
        "created_at" => array:1 [
          "$gte" => MongoDB\BSON\UTCDateTime {#926
            +"milliseconds": "1625097600000"
          }
        ]
      ]
    ]
    1 => array:1 [
      "$group" => array:2 [
        "_id" => array:1 [
          "date" => "$date"
        ]
        "count(*)" => array:1 [
          "$sum" => 1
        ]
      ]
    ]
    2 => array:1 [
      "$project" => array:3 [
        "date" => "$_id.date"
        "count(*)" => "$count(*)"
        "_id" => 0
      ]
    ]
    3 => array:1 [
      "$match" => array:1 [
        "count(*)" => array:1 [
          "$gt" => 100
        ]
      ]
    ]
  ]
  +collection: "clicks"
  +hint: "status_1_created_at_1"
}
*/