承接 tico/mongo-sql-where 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tico/mongo-sql-where

最新稳定版本:v1.0.1

Composer 安装命令:

composer require tico/mongo-sql-where

包简介

Convert MongoDB-style queries into SQL WHERE clauses in PHP

README 文档

README

Convert MongoDB-style query syntax into SQL WHERE clauses in PHP.

Features

  • Supports $or, $and, $not, $in, $nin, $gt, $lt, $gte, $lte, $ne, $regex
  • Column mapping for aliasing logical keys to SQL columns
  • No ORM required

Installation

composer require tico/mongo-sql-where

Usage

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);

Example Output

WHERE (users.status = 'active' OR products.qty < 50) AND NOT (products.category IN ('banned', 'restricted'))

Regex Support

You can use the $regex operator to perform pattern matching. Case-insensitive matching is supported via the $options parameter.

// 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);

Example Output

WHERE (users.status = 'active' OR users.name REGEXP '^A') AND (LOWER(users.email) REGEXP LOWER('@example\\\.com$'))

License

MIT

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-17