PHP code example of andydune / mongo-query

1. Go to this page and download the library: Download andydune/mongo-query library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andydune / mongo-query example snippets


$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

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


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

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

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

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


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


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

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

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

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

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

$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

$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

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

php composer.phar 

php composer.phar update