Skip to content

Commit 0517dd6

Browse files
committed
Merge pull request mongodb#5 from hannesvdvreken/master
increment, decrement, whereNotIn
2 parents 523c5f3 + 6992992 commit 0517dd6

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

Diff for: .travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ branches:
77
php:
88
- 5.3
99
- 5.4
10+
- 5.5
1011

1112
services: mongodb
1213

Diff for: README.md

+29
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ $users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();
9797
$users = User::whereIn('age', array(16, 18, 20))->get();
9898
```
9999

100+
When using `whereNotIn` objects will be returned if the field is non existant. Combine with `whereNotNull('age')` to leave out those documents.
101+
100102
**Using Where Between**
101103

102104
```php
@@ -181,3 +183,30 @@ $user = Comment::where('body', 'like', '%spam%')->get();
181183
**Inserts, updates and deletes**
182184

183185
All basic insert, update, delete and select methods should be implemented.
186+
187+
**Increments & decrements**
188+
189+
Perform increments (default 1) on specified attributes.
190+
Attention: without a where-clause, every object will be modified.
191+
192+
```php
193+
User::where('name', 'John Doe')->increment('age');
194+
User::where('name', 'Bart De Wever')->decrement('weight', 50);
195+
```
196+
197+
The number of updated objects is returned.
198+
199+
```php
200+
$count = User->increment('age');
201+
echo $count;
202+
```
203+
204+
will return the number of users where `age` is a valid field.
205+
206+
These functions also allow for a third attribute:
207+
208+
```php
209+
User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
210+
211+
User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));
212+
```

Diff for: src/Jenssegers/Mongodb/Builder.php

+50
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,49 @@ public function update(array $values)
286286
return 0;
287287
}
288288

289+
/**
290+
* Increment a column's value by a given amount.
291+
*
292+
* @param string $column
293+
* @param int $amount
294+
* @param array $extra
295+
* @return int
296+
*/
297+
public function increment($column, $amount = 1, array $extra = array())
298+
{
299+
// build update statement
300+
$update = array(
301+
'$inc' => array($column => $amount),
302+
'$set' => $extra,
303+
);
304+
305+
// protect
306+
$this->whereNotNull($column);
307+
308+
// perform
309+
$result = $this->collection->update($this->compileWheres(), $update, array('multiple' => true));
310+
311+
if (1 == (int) $result['ok'])
312+
{
313+
return $result['n'];
314+
}
315+
316+
return 0;
317+
}
318+
319+
/**
320+
* Decrement a column's value by a given amount.
321+
*
322+
* @param string $column
323+
* @param int $amount
324+
* @param array $extra
325+
* @return int
326+
*/
327+
public function decrement($column, $amount = 1, array $extra = array())
328+
{
329+
return $this->increment($column, -1 * $amount, $extra);
330+
}
331+
289332
/**
290333
* Delete a record from the database.
291334
*
@@ -429,6 +472,13 @@ private function compileWhereIn($where)
429472
return array($column => array('$in' => $values));
430473
}
431474

475+
private function compileWhereNotIn($where)
476+
{
477+
extract($where);
478+
479+
return array($column => array('$nin' => $values));
480+
}
481+
432482
private function compileWhereNull($where)
433483
{
434484
$where['operator'] = '=';

Diff for: tests/QueryTest.php

+50
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ public function testSelect()
112112
$this->assertEquals('John Doe', $user->name);
113113
$this->assertEquals(null, $user->age);
114114

115+
$user = User::select('name', 'title')->first();
116+
117+
$this->assertEquals('John Doe', $user->name);
118+
$this->assertEquals('admin', $user->title);
119+
$this->assertEquals(null, $user->age);
120+
115121
$user = User::get(array('name'))->first();
116122

117123
$this->assertEquals('John Doe', $user->name);
@@ -143,12 +149,22 @@ public function testIn()
143149

144150
$users = User::whereIn('age', array(33, 35, 13))->get();
145151
$this->assertEquals(6, count($users));
152+
153+
$users = User::whereNotIn('age', array(33, 35))->get();
154+
$this->assertEquals(4, count($users));
155+
156+
$users = User::whereNotNull('age')
157+
->whereNotIn('age', array(33, 35))->get();
158+
$this->assertEquals(3, count($users));
146159
}
147160

148161
public function testWhereNull()
149162
{
150163
$users = User::whereNull('age')->get();
151164
$this->assertEquals(1, count($users));
165+
166+
$users = User::whereNotNull('age')->get();
167+
$this->assertEquals(8, count($users));
152168
}
153169

154170
public function testOrder()
@@ -173,6 +189,37 @@ public function testOffset()
173189
$this->assertEquals('Jane Doe', $users[0]->name);
174190
}
175191

192+
public function testIncrements()
193+
{
194+
User::where('name', 'John Doe')->increment('age');
195+
User::where('name', 'John Doe')->increment('age', 2, array('title' => 'user'));
196+
197+
$user = User::where('name', 'John Doe')->first();
198+
$this->assertEquals(38, $user->age);
199+
$this->assertEquals('user', $user->title);
200+
201+
User::where('name', 'John Doe')->decrement('age');
202+
$num = User::where('name', 'John Doe')->decrement('age', 2, array('title' => 'admin'));
203+
204+
$user = User::where('name', 'John Doe')->first();
205+
$this->assertEquals(35, $user->age);
206+
$this->assertEquals('admin', $user->title);
207+
$this->assertEquals(1, $num);
208+
209+
User::increment('age');
210+
User::increment('age', 2);
211+
212+
$user = User::where('name', 'Mark Moe')->first();
213+
$this->assertEquals(26, $user->age);
214+
215+
User::decrement('age', 2);
216+
$num = User::decrement('age');
217+
218+
$user = User::where('name', 'Mark Moe')->first();
219+
$this->assertEquals(23, $user->age);
220+
$this->assertEquals(8, $num);
221+
}
222+
176223
public function testAggregates()
177224
{
178225
$this->assertEquals(9, User::count());
@@ -183,6 +230,9 @@ public function testAggregates()
183230

184231
$this->assertEquals(35, User::where('title', 'admin')->max('age'));
185232
$this->assertEquals(37, User::where('title', 'user')->max('age'));
233+
234+
$this->assertEquals(33, User::where('title', 'admin')->min('age'));
235+
$this->assertEquals(13, User::where('title', 'user')->min('age'));
186236
}
187237

188238
public function testGroupBy()

0 commit comments

Comments
 (0)