andydune/mongo-query

Add beauty to momgodb query arrays.

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

This package is auto-updated.

Last update: 2024-12-08 14:17:07 UTC


README

Build Status Software License Packagist Version Total Downloads

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