diff --git a/README.md b/README.md index 9024bb782..608241fe5 100644 --- a/README.md +++ b/README.md @@ -276,17 +276,17 @@ Or you can access the internal object directly: **Push** -Add one or more items to an array. +Add an items to an array. DB::collection('users')->where('name', 'John')->push('items', 'boots'); - DB::collection('users')->where('name', 'John')->push('items', array('sword', 'shield')); + DB::collection('users')->where('name', 'John')->push('messages', array('from' => 'Jane Doe', 'message' => 'Hi John')); **Pull** -Remove one or more values from an array. +Remove an item from an array. DB::collection('users')->where('name', 'John')->pull('items', 'boots'); - DB::collection('users')->where('name', 'John')->pull('items', array('sword', 'shield')); + DB::collection('users')->where('name', 'John')->pull('messages', array('from' => 'Jane Doe', 'message' => 'Hi John')); **Unset** diff --git a/src/Jenssegers/Mongodb/Builder.php b/src/Jenssegers/Mongodb/Builder.php index 9c9e3df86..dfa67447a 100644 --- a/src/Jenssegers/Mongodb/Builder.php +++ b/src/Jenssegers/Mongodb/Builder.php @@ -458,11 +458,6 @@ public function push($column, $value = null) { $query = array('$push' => $column); } - else if (is_array($value)) - { - // $pushAll depricated - $query = array('$push' => array($column => array('$each' => $value))); - } else { $query = array('$push' => array($column => $value)); @@ -484,10 +479,6 @@ public function pull($column, $value = null) { $query = array('$pull' => $column); } - else if (is_array($value)) - { - $query = array('$pullAll' => array($column => $value)); - } else { $query = array('$pull' => array($column => $value)); diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index e838f8390..14c0636c0 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -196,7 +196,8 @@ public function testPush() { $id = DB::collection('users')->insertGetId(array( 'name' => 'John Doe', - 'tags' => array() + 'tags' => array(), + 'messages' => array(), )); DB::collection('users')->where('_id', $id)->push('tags', 'tag1'); @@ -213,19 +214,22 @@ public function testPush() $this->assertEquals(2, count($user['tags'])); $this->assertEquals('tag2', $user['tags'][1]); - DB::collection('users')->where('_id', $id)->push('tags', array('tag3', 'tag4')); - + $message = array('from' => 'Jane', 'body' => 'Hi John'); + DB::collection('users')->where('_id', $id)->push('messages', $message); + $user = DB::collection('users')->find($id); - $this->assertTrue(is_array($user['tags'])); - $this->assertEquals(4, count($user['tags'])); - $this->assertEquals('tag4', $user['tags'][3]); + $this->assertTrue(is_array($user['messages'])); + $this->assertEquals($message, $user['messages'][0]); } public function testPull() { + $message = array('from' => 'Jane', 'body' => 'Hi John'); + $id = DB::collection('users')->insertGetId(array( 'name' => 'John Doe', - 'tags' => array('tag1', 'tag2', 'tag3', 'tag4') + 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'), + 'messages' => array($message) )); DB::collection('users')->where('_id', $id)->pull('tags', 'tag3'); @@ -235,12 +239,11 @@ public function testPull() $this->assertEquals(3, count($user['tags'])); $this->assertEquals('tag4', $user['tags'][2]); - DB::collection('users')->where('_id', $id)->pull('tags', array('tag2', 'tag4')); - + DB::collection('users')->where('_id', $id)->pull('messages', $message); + $user = DB::collection('users')->find($id); - $this->assertTrue(is_array($user['tags'])); - $this->assertEquals(1, count($user['tags'])); - $this->assertEquals('tag1', $user['tags'][0]); + $this->assertTrue(is_array($user['messages'])); + $this->assertEquals(0, count($user['messages'])); } public function testDistinct()