Skip to content

Commit

Permalink
First commit!
Browse files Browse the repository at this point in the history
  • Loading branch information
TCB13 committed Jan 25, 2019
0 parents commit 3bff94e
Show file tree
Hide file tree
Showing 7 changed files with 1,014 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor
/examples/vendor
composer.lock
/examples/composer.lock
.idea/
167 changes: 167 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# Seguel 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 worlds!

### Installation

Pull the package via composer.
```shell
$ composer require TCB13/sequel-mongo-php
```

### Usage

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

```php
// 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**:

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

**Get multiple items**:

```php
$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`.
```php
// 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:
```php
->where("id", 1)
->where(function ($qb) {
$qb->where("isValid", true)
->orWhere("dateActivated", "!=", null);
})
```
- `where()` supports the following operators `=`, `!=`, `>`, `>=`, `<`, `<=`;
- SQL's `WHERE IN()` is also available:
```php
$qb = new QueryBuilder($mongo);
$qb->collection("Orders")
->whereIn("itemCount", [22,4])
->findAll();
$result = $qb->toArray();
```
- `WHERE NOT IN()` is also available via `whereNotIn()` and `orWhereNotIn()` respectively;

- Examples of other useful String queries:
```php
->whereStartsWith("item", "start")
->whereEndsWith("item", "end")
->whereContains("item", "-middle-")
->whereRegex("item", ".*-middle-.*")
```
- For more complex String queries you may also use regex:
```php
->whereRegex("item", ".*-middle-.*")
```

**Insert a document**:
```php
$qb = new QueryBuilder($mongo);
$result = $qb->collection("TestCollection")
->insert([
"item" => "test-insert",
"xpto" => microtime(true)
]);
var_dump($result);
```
- You may also insert multiple documents at once:
```php
$items = [
[
"item" => "test-insert-multi",
"xpto" => microtime(true)
],
[
"item" => "test-insert-multi",
"xpto" => microtime(true)
]
];

$qb = new QueryBuilder($mongo);
$result = $qb->collection("TestCollection")
->insert($items);
```

**Delete a Document**:
```php
$qb = new QueryBuilder($mongo);
$result = $qb->collection("TestCollection")
->whereStartsWith("item", "test-insert-")
->delete();
var_dump($result);
```

**Update a Document**:
```php
// Update item
$qb = new QueryBuilder($mongo);
$result = $qb->collection($collection)
->where("_id", new MongoID("51ee74e944670a09028d4fc9"))
->update([
"item" => "updated-value " . microtime(true)
]);
var_dump($result);
```
- You may update only a few fields or an entire document - like like an SQL `update` statement.

**Join Collections**:

_**join($collectionToJoin, $localField, $operatorOrForeignField, $foreignField)**_
```php
$qb = new QueryBuilder($mongo);
$qb->collection("Orders")
//->select("_id", "products#joined.sku")
//->join(["products" => "products#joined"], "sku", "=", "item"])
//->join("products", "sku", "=", "item")
->join("Products", "sku", "item")
->findAll();
$result = $qb->toArray();
var_dump($result);
```

**For more examples check out the `examples` directory.**
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "tcb13/thunder-tus-php",
"type": "library",
"description": "",
"require": {
"php": "^7.2",
"ext-json": "*",
"ext-curl": "*",
"ext-mongodb": "*",
"mongodb/mongodb": "1.4.*"
},
"authors": [
{
"name": "TCB13",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"SequelMongo\\": "src/"
}
}
}
Loading

0 comments on commit 3bff94e

Please sign in to comment.