Skip to content

Commit ac1bd36

Browse files
committed
Added , fixes #34
1 parent 5b93d89 commit ac1bd36

File tree

5 files changed

+158
-31
lines changed

5 files changed

+158
-31
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,17 @@ Remove one or more values from an array.
288288
DB::collection('users')->where('name', 'John')->pull('items', 'boots');
289289
DB::collection('users')->where('name', 'John')->pull('items', array('sword', 'shield'));
290290

291+
**Unset**
292+
293+
Remove one or more fields from a document.
294+
295+
DB::collection('users')->where('name', 'John')->unset('note');
296+
297+
You can also perform an unset on a model.
298+
299+
$user = User::where('name', 'John')->first();
300+
$user->unset('note');
301+
291302
### Query Caching
292303

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

src/Jenssegers/Mongodb/Builder.php

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
class Builder extends \Illuminate\Database\Query\Builder {
88

99
/**
10-
* The database collection
11-
*
12-
* @var MongoCollection
13-
*/
10+
* The database collection
11+
*
12+
* @var MongoCollection
13+
*/
1414
protected $collection;
1515

1616
/**
17-
* All of the available operators.
18-
*
19-
* @var array
20-
*/
17+
* All of the available operators.
18+
*
19+
* @var array
20+
*/
2121
protected $conversion = array(
2222
'=' => '=',
2323
'!=' => '$ne',
@@ -29,11 +29,11 @@ class Builder extends \Illuminate\Database\Query\Builder {
2929
);
3030

3131
/**
32-
* Create a new query builder instance.
33-
*
34-
* @param Connection $connection
35-
* @return void
36-
*/
32+
* Create a new query builder instance.
33+
*
34+
* @param Connection $connection
35+
* @return void
36+
*/
3737
public function __construct(Connection $connection)
3838
{
3939
$this->connection = $connection;
@@ -339,24 +339,15 @@ public function update(array $values, array $options = array())
339339
*/
340340
public function increment($column, $amount = 1, array $extra = array())
341341
{
342-
// build update statement
343-
$update = array(
342+
$query = array(
344343
'$inc' => array($column => $amount),
345344
'$set' => $extra,
346345
);
347346

348-
// protect
347+
// Protect
349348
$this->whereNotNull($column);
350349

351-
// perform
352-
$result = $this->collection->update($this->compileWheres(), $update, array('multiple' => true));
353-
354-
if (1 == (int) $result['ok'])
355-
{
356-
return $result['n'];
357-
}
358-
359-
return 0;
350+
return $this->performUpdate($query);
360351
}
361352

362353
/**
@@ -505,6 +496,28 @@ public function pull($column, $value = null)
505496
return $this->performUpdate($query);
506497
}
507498

499+
/**
500+
* Remove one or more fields.
501+
*
502+
* @param mixed $columns
503+
* @return int
504+
*/
505+
public function dropColumn($columns)
506+
{
507+
if (!is_array($columns)) $columns = array($columns);
508+
509+
$fields = array();
510+
511+
foreach ($columns as $column)
512+
{
513+
$fields[$column] = 1;
514+
}
515+
516+
$query = array('$unset' => $fields);
517+
518+
return $this->performUpdate($query);
519+
}
520+
508521
/**
509522
* Get a new instance of the query builder.
510523
*
@@ -516,7 +529,7 @@ public function newQuery()
516529
}
517530

518531
/**
519-
* Perform update.
532+
* Perform an update query.
520533
*
521534
* @param array $query
522535
* @param array $options
@@ -541,7 +554,7 @@ protected function performUpdate($query, array $options = array())
541554
}
542555

543556
/**
544-
* Convert a key to MongoID if needed
557+
* Convert a key to MongoID if needed.
545558
*
546559
* @param mixed $id
547560
* @return mixed
@@ -557,10 +570,10 @@ protected function convertKey($id)
557570
}
558571

559572
/**
560-
* Compile the where array
561-
*
562-
* @return array
563-
*/
573+
* Compile the where array.
574+
*
575+
* @return array
576+
*/
564577
protected function compileWheres()
565578
{
566579
if (!$this->wheres) return array();
@@ -694,4 +707,21 @@ protected function compileWhereRaw($where)
694707
return $where['sql'];
695708
}
696709

710+
/**
711+
* Handle dynamic method calls into the method.
712+
*
713+
* @param string $method
714+
* @param array $parameters
715+
* @return mixed
716+
*/
717+
public function __call($method, $parameters)
718+
{
719+
if ($method == 'unset')
720+
{
721+
return call_user_func_array(array($this, 'dropColumn'), $parameters);
722+
}
723+
724+
return parent::__call($method, $parameters);
725+
}
726+
697727
}

src/Jenssegers/Mongodb/Model.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,41 @@ public function setRawAttributes(array $attributes, $sync = false)
213213
parent::setRawAttributes($attributes, $sync);
214214
}
215215

216+
/**
217+
* Remove one or more fields.
218+
*
219+
* @param mixed $columns
220+
* @return int
221+
*/
222+
public function dropColumn($columns)
223+
{
224+
if (!is_array($columns)) $columns = array($columns);
225+
226+
// Unset attributes
227+
foreach ($columns as $column)
228+
{
229+
$this->__unset($column);
230+
}
231+
232+
// Perform unset only on current document
233+
return $query = $this->newQuery()->where($this->getKeyName(), $this->getKey())->unset($columns);
234+
}
235+
236+
/**
237+
* Handle dynamic method calls into the method.
238+
*
239+
* @param string $method
240+
* @param array $parameters
241+
* @return mixed
242+
*/
243+
public function __call($method, $parameters)
244+
{
245+
if ($method == 'unset')
246+
{
247+
return call_user_func_array(array($this, 'dropColumn'), $parameters);
248+
}
249+
250+
return parent::__call($method, $parameters);
251+
}
252+
216253
}

tests/ModelTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,31 @@ public function testToArray()
278278
$this->assertEquals($original[0], $items[0]->toArray());
279279
}
280280

281+
public function testUnset()
282+
{
283+
$user1 = User::create(array('name' => 'John Doe', 'note1' => 'ABC', 'note2' => 'DEF'));
284+
$user2 = User::create(array('name' => 'Jane Doe', 'note1' => 'ABC', 'note2' => 'DEF'));
285+
286+
$user1->unset('note1');
287+
288+
$this->assertFalse(isset($user1->note1));
289+
$this->assertTrue(isset($user1->note2));
290+
$this->assertTrue(isset($user2->note1));
291+
$this->assertTrue(isset($user2->note2));
292+
293+
// Re-fetch to be sure
294+
$user1 = User::find($user1->_id);
295+
$user2 = User::find($user2->_id);
296+
297+
$this->assertFalse(isset($user1->note1));
298+
$this->assertTrue(isset($user1->note2));
299+
$this->assertTrue(isset($user2->note1));
300+
$this->assertTrue(isset($user2->note2));
301+
302+
$user2->unset(array('note1', 'note2'));
303+
304+
$this->assertFalse(isset($user2->note1));
305+
$this->assertFalse(isset($user2->note2));
306+
}
307+
281308
}

tests/QueryBuilderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,26 @@ public function testUpsert()
383383
$this->assertEquals(1, DB::collection('items')->count());
384384
}
385385

386+
public function testUnset()
387+
{
388+
$id1 = DB::collection('users')->insertGetId(array('name' => 'John Doe', 'note1' => 'ABC', 'note2' => 'DEF'));
389+
$id2 = DB::collection('users')->insertGetId(array('name' => 'Jane Doe', 'note1' => 'ABC', 'note2' => 'DEF'));
390+
391+
DB::collection('users')->where('name', 'John Doe')->unset('note1');
392+
393+
$user1 = DB::collection('users')->find($id1);
394+
$user2 = DB::collection('users')->find($id2);
395+
396+
$this->assertFalse(isset($user1['note1']));
397+
$this->assertTrue(isset($user1['note2']));
398+
$this->assertTrue(isset($user2['note1']));
399+
$this->assertTrue(isset($user2['note2']));
400+
401+
DB::collection('users')->where('name', 'Jane Doe')->unset(array('note1', 'note2'));
402+
403+
$user2 = DB::collection('users')->find($id2);
404+
$this->assertFalse(isset($user2['note1']));
405+
$this->assertFalse(isset($user2['note2']));
406+
}
407+
386408
}

0 commit comments

Comments
 (0)