andydune/mongo-query

为MongoDB查询数组增添美感。

v1.1.1 2019-06-18 05:43 UTC

This package is auto-updated.

Last update: 2024-09-08 13:50:35 UTC


README

Build Status Software License Packagist Version Total Downloads

为MongoDB查询数组增添美感。减少错误,减少括号,更易于理解。它不是ORM或ODM,它只是一个构建器。因此,您可以自由地单独使用它或与任何ORM(如mongolid)一起使用。

要求

  • PHP版本 >= 7.1
  • 冰箱里有一两瓶啤酒。

什么是美感

最初看起来像

$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会改变它

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

安装

使用composer进行安装

composer require andydune/mongo-query

如果composer没有全局安装

php composer.phar require andydune/mongo-query

或编辑您的 composer.json

"require" : {
     "andydune/mongo-query": "^1"
}

然后执行命令

php composer.phar update

执行

您可以使用 findfindOne 方法

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

美感元素

不是

操作符为链中的下一个操作符创建负条件。

重要! 它不适用于所有操作符。

原始

$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

更美

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

操作可以与 not 修饰符一起使用。

$cursor = $collection->find((new Query)->field('type')->not()->in('virginia', 'latakia')->get());

之间

原始

$collection = (new MongoDB\Client)->base->tobacco;
$cursor = $collection->find(
['$and' => [
    ['price' => ['$gt' => 10]],
    ['price' => ['$lt' => 100]]
]]);

更美

$collection = (new MongoDB\Client)->test->tobacco;
$cursor = $collection->find(
(new Query)->field('price')->between->(10, 100)->get()
);

操作可以与 not 修饰符一起使用。

(new Query)->field('price')->not()->between->(10, 100)->get()

eq

匹配等于指定值的值。

(new Query)->field('price')->eq->(10)->get(); // ['price' => ['$eq' => 10]]

操作不能与 not 修饰符一起使用。这是一个特殊方法 ne

ne

匹配所有不等于指定值的值。

(new Query)->field('price')->ne->(10)->get(); // ['price' => ['$ne' => 10]]

操作不能与 not 修饰符一起使用。

gt 和 lt

用于 $gt$lt 比较的运算符。

(new Query)->field('price')->lt->(10)->get();

(new Query)->field('price')->gt->(100)->get();

操作不能与 not 修饰符一起使用。

嵌套查询

查询对象可以是新查询的条件。为此,有一个 addQuery 方法。

$query = new Query();
$query->field('price')->gt(80);

$queryAdd = new Query();
$queryAdd->field('price')->lt(100);

$data = $query->addQuery($queryAdd)->get();

数据类型修正

使用与集合中数据类型相同的参数查询数据非常重要。因此,1 != '1'

Query 构造函数可以接收带有字段元描述的数组。

$query = new Query([
   'price' => 'int',
   'type' => 'string',
]);
$queryArray = $queryAdd->field('price')->gt(false)->get();

// after all:
$queryArray = [
   'price' => ['$gt' => 0] // (int)false -> 0
];

日期时间类型

您可以使用简单类型来表示日期时间。

整数值用作时间戳

$query = new Query(['date' => 'datetime']);
$query->field('date')->lt(time() - 60); // documents with date 60 seconds old

字符串必须是 Y-m-d H:i:s 格式

$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

带有前缀 -+ 的字符串

$query = new Query(['date' => 'datetime']);
$query->field('date')->between('- 5 days', '+ 5 minutes'); // 5 days before and 5 minutes to future

使用类型 AndyDune\DateTime\DateTime

use AndyDune\DateTime\DateTime;
$query = new Query(['date' => 'datetime']);
$query->field('date')->eq(new DateTime());