PHP code example of tico / mongo-sql-where
1. Go to this page and download the library: Download tico/mongo-sql-where 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/ */
tico / mongo-sql-where example snippets
use Tico\MongoSqlWhere\MongoQueryToSql;
$query = [
'$or' => [
['status' => 'active'],
['qty' => ['$lt' => 50]]
],
'$not' => ['category' => ['$in' => ['banned', 'restricted']]]
];
$map = [
'status' => 'users.status',
'qty' => 'products.qty',
'category' => 'products.category'
];
$converter = new MongoQueryToSql($map);
echo $converter->convert($query);
// Simple regex
$query = ['name' => ['$regex' => '^test']];
// Converts to: WHERE name REGEXP '^test'
// Case-insensitive regex
$query = [
'email' => [
'$regex' => '@example\\.com$',
'$options' => 'i' // 'i' for case-insensitive
]
];
// Converts to: WHERE LOWER(email) REGEXP LOWER('@example\\.com$')
// In complex queries
$query = [
'status' => 'active',
'$or' => [
['name' => ['$regex' => '^A']],
['email' => ['$regex' => '@example\\.com$', '$options' => 'i']]
]
];
$map = [
'status' => 'users.status',
'name' => 'users.name',
'email' => 'users.email'
];
$converter = new MongoQueryToSql($map);
echo $converter->convert($query);