Skip to content

Commit 9a2a82d

Browse files
committed
Adding pull support
1 parent dd9e2a5 commit 9a2a82d

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ Add one or more items to an array.
252252
User::where('name', 'John')->push('items', 'boots');
253253
User::where('name', 'John')->push('items', array('sword', 'shield'));
254254

255+
***Pull***
256+
257+
Remove one or more values from an array.
258+
259+
User::where('name', 'John')->pull('items', 'boots');
260+
User::where('name', 'John')->pull('items', array('sword', 'shield'));
261+
255262
### Query Caching
256263

257264
You may easily cache the results of a query using the remember method:

src/Jenssegers/Mongodb/Builder.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,9 @@ public function raw($expression = null)
412412
}
413413

414414
/**
415-
* Append a value to an array.
415+
* Append one or more values to an array.
416416
*
417-
* @param string $column
417+
* @param mixed $column
418418
* @param mixed $value
419419
* @return int
420420
*/
@@ -426,6 +426,7 @@ public function push($column, $value = null)
426426
}
427427
else if (is_array($value))
428428
{
429+
// $pushAll depricated
429430
$query = array('$push' => array($column => array('$each' => $value)));
430431
}
431432
else
@@ -436,6 +437,31 @@ public function push($column, $value = null)
436437
return $this->performUpdate($query);
437438
}
438439

440+
/**
441+
* Remove one or more values from an array.
442+
*
443+
* @param mixed $column
444+
* @param mixed $value
445+
* @return int
446+
*/
447+
public function pull($column, $value = null)
448+
{
449+
if (is_array($column))
450+
{
451+
$query = array('$pull' => $column);
452+
}
453+
else if (is_array($value))
454+
{
455+
$query = array('$pullAll' => array($column => $value));
456+
}
457+
else
458+
{
459+
$query = array('$pull' => array($column => $value));
460+
}
461+
462+
return $this->performUpdate($query);
463+
}
464+
439465
/**
440466
* Get a new instance of the query builder.
441467
*

tests/QueryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,24 @@ public function testPush()
128128
$this->assertEquals('tag4', $user['tags'][3]);
129129
}
130130

131+
public function testPull()
132+
{
133+
$user = array('name' => 'John Doe', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'));
134+
$id = DB::collection('users')->insertGetId($user);
135+
136+
DB::collection('users')->where('_id', $id)->pull('tags', 'tag3');
137+
$user = DB::collection('users')->find($id);
138+
139+
$this->assertTrue(is_array($user['tags']));
140+
$this->assertEquals(3, count($user['tags']));
141+
$this->assertEquals('tag4', $user['tags'][2]);
142+
143+
DB::collection('users')->where('_id', $id)->pull('tags', array('tag2', 'tag4'));
144+
$user = DB::collection('users')->find($id);
145+
146+
$this->assertTrue(is_array($user['tags']));
147+
$this->assertEquals(1, count($user['tags']));
148+
$this->assertEquals('tag1', $user['tags'][0]);
149+
}
150+
131151
}

0 commit comments

Comments
 (0)