PHPackages                             andydune/mongo-query - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. andydune/mongo-query

ActiveLibrary[Database &amp; ORM](/categories/database)

andydune/mongo-query
====================

Add beauty to momgodb query arrays.

v1.1.1(6y ago)1318MITPHPPHP &gt;=7.1CI failing

Since Oct 12Pushed 5y ago1 watchersCompare

[ Source](https://github.com/AndyDune/MongoQuery)[ Packagist](https://packagist.org/packages/andydune/mongo-query)[ Docs](https://github.com/AndyDune/MongoQuery)[ RSS](/packages/andydune-mongo-query/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (16)Used By (0)

MongoQuery
==========

[](#mongoquery)

[![Build Status](https://camo.githubusercontent.com/056bf94cdf96ab2f9ba2f1fbbc5eb377bfca30e7721932e9e3beb6b9d3c4db9c/68747470733a2f2f7472617669732d63692e6f72672f416e647944756e652f4d6f6e676f51756572792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/AndyDune/MongoQuery)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Packagist Version](https://camo.githubusercontent.com/625559659e61c4513c4510f498224008b58d3cd538afb9c864f73660e998ae78/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e647964756e652f6d6f6e676f2d71756572792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andydune/mongo-query)[![Total Downloads](https://camo.githubusercontent.com/09c72a364c6ecdf51e06b3b915f0645e0a7427f609b5d2df5b11229ccd2f2936/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e647964756e652f6d6f6e676f2d71756572792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andydune/mongo-query)

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](https://github.com/leroy-merlin-br/mongolid)

Requirements
------------

[](#requirements)

- PHP version &gt;= 7.1
- A couple of beers in the fridge.

What is beauty
--------------

[](#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)

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

[](#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
------------------

[](#elements-of-beauty)

### Not

[](#not)

Operator make negative condition for right next operator in chain.

*Important!* It is not suitable for all operators.

### In

[](#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

[](#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

[](#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

[](#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

[](#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
--------------

[](#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
--------------------

[](#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

[](#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());
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~44 days

Recently: every ~110 days

Total

15

Last Release

2518d ago

Major Versions

v0.8.1 → v1.0.02018-04-05

PHP version history (3 changes)v0.1.0PHP ^7.0 || ^7.1

v0.6.0PHP ^7.0 || ^7.1 || ^7.2

v0.7.1PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/79da3b2173a2cefb36abc9b4707cf2c633df8f2c748633ccf64186f5c0e7be6c?d=identicon)[AndyDune](/maintainers/AndyDune)

---

Top Contributors

[![AndyDune](https://avatars.githubusercontent.com/u/3772910?v=4)](https://github.com/AndyDune "AndyDune (59 commits)")

---

Tags

mongomongodbphpquery-builderphpquery buildermongo

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/andydune-mongo-query/health.svg)

```
[![Health](https://phpackages.com/badges/andydune-mongo-query/health.svg)](https://phpackages.com/packages/andydune-mongo-query)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[denchikby/phalcon-mongodb-odm

Phalcon MongoDB ODM

4212.8k](/packages/denchikby-phalcon-mongodb-odm)[matchory/elasticsearch

The missing elasticsearch ORM for Laravel!

3059.0k](/packages/matchory-elasticsearch)[hemiframe/php-query-builder

Powerful and lightweight PHP SQL Query Builder

1018.2k](/packages/hemiframe-php-query-builder)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
