Skip to content

Commit 3bff94e

Browse files
author
TCB13
committed
First commit!
0 parents  commit 3bff94e

File tree

7 files changed

+1014
-0
lines changed

7 files changed

+1014
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor
2+
/examples/vendor
3+
composer.lock
4+
/examples/composer.lock
5+
.idea/

Readme.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Seguel Mongo PHP
2+
3+
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!
4+
5+
### Installation
6+
7+
Pull the package via composer.
8+
```shell
9+
$ composer require TCB13/sequel-mongo-php
10+
```
11+
12+
### Usage
13+
14+
**Use `MongoDB\Client` to connect to your Database**:
15+
16+
```php
17+
// Start a MongoDB Client to create a connection to your Database
18+
$mongoConnection = new \MongoDB\Client("mongodb://127.0.0.1", [
19+
"username" => "user",
20+
"password" => "pass"
21+
]);
22+
23+
/** @var \MongoDB\Database $mongo */
24+
$mongo = $mongoConnection->selectDatabase("DatabaseName");
25+
```
26+
27+
**Get one item from a collection**:
28+
29+
```php
30+
$qb = new QueryBuilder($mongo);
31+
$qb->collection("Users")
32+
->select("_id")
33+
->find();
34+
$result = $qb->toObject();
35+
var_dump($result);
36+
```
37+
38+
**Get multiple items**:
39+
40+
```php
41+
$qb = new QueryBuilder($mongo);
42+
$qb->collection("Users")
43+
->select("_id")
44+
->limit(2)
45+
->findAll();
46+
$result = $qb->toObject();
47+
var_dump($result);
48+
```
49+
50+
- `select()` takes the desired fields as an `array` of strings or a variable number of parameters.
51+
52+
**Run a complex query** with multiple `where` conditions, `limit` and `order`.
53+
```php
54+
// Use the Query Builder
55+
$qb = new QueryBuilder($mongo);
56+
$qb->collection("Users")
57+
->select("_id", "name", "email", "active")
58+
->where("setting", "!=", "other")
59+
->where("active", false)
60+
->where(function ($qb) {
61+
$qb->where("isValid", true)
62+
->orWhere("dateActivated", "!=", null);
63+
})
64+
->limit(100)
65+
->order("dateCreated", "ASC")
66+
->findAll();
67+
$result = $qb->toObject();
68+
var_dump($result);
69+
```
70+
- If the operator is omitted in `where()` queries, `=` will be used. Eg. `->where("active", false)`;
71+
- You can group `where` conditions by providing closures. Eg. `WHERE id = 1 AND (isValid = true OR dateActivated != null)` can be written as:
72+
```php
73+
->where("id", 1)
74+
->where(function ($qb) {
75+
$qb->where("isValid", true)
76+
->orWhere("dateActivated", "!=", null);
77+
})
78+
```
79+
- `where()` supports the following operators `=`, `!=`, `>`, `>=`, `<`, `<=`;
80+
- SQL's `WHERE IN()` is also available:
81+
```php
82+
$qb = new QueryBuilder($mongo);
83+
$qb->collection("Orders")
84+
->whereIn("itemCount", [22,4])
85+
->findAll();
86+
$result = $qb->toArray();
87+
```
88+
- `WHERE NOT IN()` is also available via `whereNotIn()` and `orWhereNotIn()` respectively;
89+
90+
- Examples of other useful String queries:
91+
```php
92+
->whereStartsWith("item", "start")
93+
->whereEndsWith("item", "end")
94+
->whereContains("item", "-middle-")
95+
->whereRegex("item", ".*-middle-.*")
96+
```
97+
- For more complex String queries you may also use regex:
98+
```php
99+
->whereRegex("item", ".*-middle-.*")
100+
```
101+
102+
**Insert a document**:
103+
```php
104+
$qb = new QueryBuilder($mongo);
105+
$result = $qb->collection("TestCollection")
106+
->insert([
107+
"item" => "test-insert",
108+
"xpto" => microtime(true)
109+
]);
110+
var_dump($result);
111+
```
112+
- You may also insert multiple documents at once:
113+
```php
114+
$items = [
115+
[
116+
"item" => "test-insert-multi",
117+
"xpto" => microtime(true)
118+
],
119+
[
120+
"item" => "test-insert-multi",
121+
"xpto" => microtime(true)
122+
]
123+
];
124+
125+
$qb = new QueryBuilder($mongo);
126+
$result = $qb->collection("TestCollection")
127+
->insert($items);
128+
```
129+
130+
**Delete a Document**:
131+
```php
132+
$qb = new QueryBuilder($mongo);
133+
$result = $qb->collection("TestCollection")
134+
->whereStartsWith("item", "test-insert-")
135+
->delete();
136+
var_dump($result);
137+
```
138+
139+
**Update a Document**:
140+
```php
141+
// Update item
142+
$qb = new QueryBuilder($mongo);
143+
$result = $qb->collection($collection)
144+
->where("_id", new MongoID("51ee74e944670a09028d4fc9"))
145+
->update([
146+
"item" => "updated-value " . microtime(true)
147+
]);
148+
var_dump($result);
149+
```
150+
- You may update only a few fields or an entire document - like like an SQL `update` statement.
151+
152+
**Join Collections**:
153+
154+
_**join($collectionToJoin, $localField, $operatorOrForeignField, $foreignField)**_
155+
```php
156+
$qb = new QueryBuilder($mongo);
157+
$qb->collection("Orders")
158+
//->select("_id", "products#joined.sku")
159+
//->join(["products" => "products#joined"], "sku", "=", "item"])
160+
//->join("products", "sku", "=", "item")
161+
->join("Products", "sku", "item")
162+
->findAll();
163+
$result = $qb->toArray();
164+
var_dump($result);
165+
```
166+
167+
**For more examples check out the `examples` directory.**

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "tcb13/thunder-tus-php",
3+
"type": "library",
4+
"description": "",
5+
"require": {
6+
"php": "^7.2",
7+
"ext-json": "*",
8+
"ext-curl": "*",
9+
"ext-mongodb": "*",
10+
"mongodb/mongodb": "1.4.*"
11+
},
12+
"authors": [
13+
{
14+
"name": "TCB13",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"minimum-stability": "dev",
19+
"autoload": {
20+
"psr-4": {
21+
"SequelMongo\\": "src/"
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)