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
- PHP version >= 7.1
- A couple of beers in the fridge.
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 onceMongoQuery 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 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
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]]);Operator make negative condition for right next operator in chain.
Important! It is not suitable for all operators.
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 bracketsMore 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());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()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
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.
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.
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();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
];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 oldString 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 nowString with prefix - or +
$query = new Query(['date' => 'datetime']);
$query->field('date')->between('- 5 days', '+ 5 minutes'); // 5 days before and 5 minutes to futureUsing type AndyDune\DateTime\DateTime
use AndyDune\DateTime\DateTime;
$query = new Query(['date' => 'datetime']);
$query->field('date')->eq(new DateTime());