PHPackages                             tcb13/sequel-mongo-php - 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. tcb13/sequel-mongo-php

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

tcb13/sequel-mongo-php
======================

1.1.19(10mo ago)82413MITPHPPHP &gt;=7.2

Since Jan 25Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/TCB13/sequel-mongo-php)[ Packagist](https://packagist.org/packages/tcb13/sequel-mongo-php)[ RSS](/packages/tcb13-sequel-mongo-php/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (10)Dependencies (1)Versions (33)Used By (0)

Sequel Mongo PHP
================

[](#sequel-mongo-php)

A lightweight, expressive, framework agnostic **query builder for PHP** that empowers you to run **SQL-like queries on MongoDB databases**. Enjoy the best of the two worlds!

### Installation

[](#installation)

Pull the package via composer.

```
$ composer require TCB13/sequel-mongo-php
```

### Usage

[](#usage)

**Use `MongoDB\Client` to connect to your Database**:

```
// Start a MongoDB Client to create a connection to your Database
$mongoConnection = new \MongoDB\Client("mongodb://127.0.0.1", [
	"username" => "user",
	"password" => "pass"
]);

/** @var \MongoDB\Database $mongo */
$mongo = $mongoConnection->selectDatabase("DatabaseName");
```

**Get one item from a collection**:

```
$qb = new QueryBuilder($mongo);
$qb->collection("Users")
   ->select("_id")
   ->find();
$result = $qb->toObject();
var_dump($result);
```

**Get multiple items**:

```
$qb = new QueryBuilder($mongo);
$qb->collection("Users")
   ->select("_id")
   ->limit(2)
   ->findAll();
$result = $qb->toObject();
var_dump($result);
```

- `select()` takes the desired fields as an `array` of strings or a variable number of parameters.

**Run a complex query** with multiple `where` conditions, `limit` and `order`.

```
// Use the Query Builder
$qb = new QueryBuilder($mongo);
$qb->collection("Users")
   ->select("_id", "name", "email", "active")
   ->where("setting", "!=", "other")
   ->where("active", false)
   ->where(function ($qb) {
	   $qb->where("isValid", true)
	      ->orWhere("dateActivated", "!=", null);
   })
   ->limit(100)
   ->order("dateCreated", "ASC")
   ->findAll();
$result = $qb->toObject();
var_dump($result);
```

- If the operator is omitted in `where()` queries, `=` will be used. Eg. `->where("active", false)`;
- You can group `where` conditions by providing closures. Eg. `WHERE id = 1 AND (isValid = true OR dateActivated != null)` can be written as:

```
->where("id", 1)
->where(function ($qb) {
    $qb->where("isValid", true)
       ->orWhere("dateActivated", "!=", null);
})
```

- `where()` supports the following operators `=`, `!=`, `>`, `>=`, `
