nddcoder/sql-to-mongodb-query

1.2.0 2023-03-02 07:53 UTC

This package is auto-updated.

Last update: 2024-04-30 00:32:06 UTC


README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Installation

You can install the package via composer:

composer require nddcoder/sql-to-mongodb-query

Online Demo

SQL To MongoDB Query

GUI phpMongoAdmin

phpMongoAdmin

Usage

Parse Find query

$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"
}

*/

Parse Aggregate query

$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"
}
*/

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.