andydune/mongo-query
Composer 安装命令:
composer require andydune/mongo-query
包简介
Add beauty to momgodb query arrays.
关键字:
README 文档
README
Add beauty to momgodb query arrays. Less errors, less brackets, more understanding. It is not ORM or ODM, it's only builder. So you may feel free to use it standalone or with any orm like mongolid
Requirements
- PHP version >= 7.1
- A couple of beers in the fridge.
What is beauty
Originally it looks like:
$collection = (new MongoDB\Client)->shop->tobacco; $cursor = $collection->find(['price' => ['$lt' => 1000]]); $collection = (new MongoDB\Client)->shop->tobacco; $cursor = $collection->find(['type' => ['$in' => ['virginia', 'latakia']]]); // 3 brackets at once
MongoQuery change it:
$collection = (new MongoDB\Client)->shop->tobacco; $cursor = $collection->find((new Query)->field('price')->lessThan(1000)->get()); // or $cursor = (new Query)->field('price')->lessThan(1000)->find($collection); $collection = (new MongoDB\Client)->test->tobacco; $cursor = $collection->find((new Query)->field('type')->in('virginia', 'latakia')->get()); // or $cursor = (new Query)->field('type')->in('virginia', 'latakia')->find($collection)
Installation
Installation using composer:
composer require andydune/mongo-query
Or if composer didn't install globally:
php composer.phar require andydune/mongo-query
Or edit your composer.json:
"require" : {
"andydune/mongo-query": "^1"
}
And execute command:
php composer.phar update
Execution
You can use methods find or findOne:
$query = new Query(); $query->field('price')->between(10, 100); $mongo = new \MongoDB\Client(); $collection = $mongo->selectDatabase('shop')->selectCollection('tobacco'); $result = $query->find($collection, ['sort' => ['name' => 1]])->toArray(); $result = $query->findOne($collection, ['sort' => ['name' => 1]]);
Elements of Beauty
Not
Operator make negative condition for right next operator in chain.
Important! It is not suitable for all operators.
In
Original:
$collection = (new MongoDB\Client)->base->tobacco; $cursor = $collection->find(['type' => ['$in' => ['virginia', 'latakia']]]); // if not $cursor = $collection->find(['type' => ['$not' => ['$in' => ['virginia', 'latakia']]]]); // to many brackets
More beauty
use AndyDune\MongoQuery\Query; $collection = (new MongoDB\Client)->test->tobacco; $cursor = $collection->find((new Query)->field('type')->in('virginia', 'latakia')->get()); //or $cursor = $collection->find((new Query)->field('type')->in(['virginia', 'latakia'])->get()); //or $cursor = $collection->find((new Query)->field('type')->in(['virginia'], 'latakia')->get());
Operation can be used with not modifier.
$cursor = $collection->find((new Query)->field('type')->not()->in('virginia', 'latakia')->get());
Between
Original:
$collection = (new MongoDB\Client)->base->tobacco; $cursor = $collection->find( ['$and' => [ ['price' => ['$gt' => 10]], ['price' => ['$lt' => 100]] ]]);
More beauty
$collection = (new MongoDB\Client)->test->tobacco; $cursor = $collection->find( (new Query)->field('price')->between->(10, 100)->get() );
Operation can be used with not modifier.
(new Query)->field('price')->not()->between->(10, 100)->get()
eq
Matches values that are equal to a specified value.
(new Query)->field('price')->eq->(10)->get(); // ['price' => ['$eq' => 10]]
Operation can not be used with not modifier. It is special method ne
ne
Matches all values that are not equal to a specified value.
(new Query)->field('price')->ne->(10)->get(); // ['price' => ['$ne' => 10]]
Operation can not be used with not modifier.
gt and lt
Operators for $gt and $lt comparision.
(new Query)->field('price')->lt->(10)->get(); (new Query)->field('price')->gt->(100)->get();
Operation can not be used with not modifier.
Nested queries
Query objects can be the conditions of new query. There is method addQuery for this.
$query = new Query(); $query->field('price')->gt(80); $queryAdd = new Query(); $queryAdd->field('price')->lt(100); $data = $query->addQuery($queryAdd)->get();
Data type correction
It's important to use as parameters for query data with type same as data in collection.
So 1 != '1'.
Query constructor can receive array with meta description for fields.
$query = new Query([ 'price' => 'int', 'type' => 'string', ]); $queryArray = $queryAdd->field('price')->gt(false)->get(); // after all: $queryArray = [ 'price' => ['$gt' => 0] // (int)false -> 0 ];
Datetime type
You can use simple types for datetime.
Integer value is use as timestamp:
$query = new Query(['date' => 'datetime']); $query->field('date')->lt(time() - 60); // documents with date 60 seconds old
String must be Y-m-d H:i:s format:
$query = new Query(['date' => 'datetime']); $query->field('date')->between(date(time() - 60, 'Y-m-d H:i:s'), time()); // documents between date 60 seconds old and now
String with prefix - or +
$query = new Query(['date' => 'datetime']); $query->field('date')->between('- 5 days', '+ 5 minutes'); // 5 days before and 5 minutes to future
Using type AndyDune\DateTime\DateTime
use AndyDune\DateTime\DateTime; $query = new Query(['date' => 'datetime']); $query->field('date')->eq(new DateTime());
andydune/mongo-query 适用场景与选型建议
andydune/mongo-query 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 318 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「mongo」 「query-builder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 andydune/mongo-query 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 andydune/mongo-query 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 andydune/mongo-query 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel, Lumen and Native php elasticseach query builder to build complex queries using an elegant syntax
GraphQL client and query builder.
GraphQL client and query builder. PHP 8.4 compatible
MongoDB extension for the Yii framework
A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
InitPHP Database — QueryBuilder, DBAL and ORM facade for the InitORM stack, plus a server-side DataTables.js helper.
统计信息
- 总下载量: 318
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-12