Skip to content

Commit ed8dad2

Browse files
committed
Fixes mongodb#418
1 parent 141871f commit ed8dad2

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use MongoId, MongoRegex, MongoDate, DateTime, Closure;
44
use Illuminate\Database\Query\Builder as BaseBuilder;
55
use Illuminate\Database\Query\Expression;
6+
use Illuminate\Support\Collection;
67
use Jenssegers\Mongodb\Connection;
78

89
class Builder extends BaseBuilder {
@@ -549,6 +550,33 @@ public function truncate()
549550
return (1 == (int) $result['ok']);
550551
}
551552

553+
/**
554+
* Get an array with the values of a given column.
555+
*
556+
* @param string $column
557+
* @param string $key
558+
* @return array
559+
*/
560+
public function lists($column, $key = null)
561+
{
562+
if ($key == '_id')
563+
{
564+
$results = new Collection($this->get([$column, $key]));
565+
566+
// Convert MongoId's to strings so that lists can do its work.
567+
$results = $results->map(function($item)
568+
{
569+
$item['_id'] = (string) $item['_id'];
570+
571+
return $item;
572+
});
573+
574+
return $results->lists($column, $key);
575+
}
576+
577+
return parent::lists($column, $key);
578+
}
579+
552580
/**
553581
* Create a raw database expression.
554582
*

tests/QueryBuilderTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ public function testList()
370370
$list = DB::collection('items')->lists('type', 'name');
371371
$this->assertEquals(3, count($list));
372372
$this->assertEquals(array('knife' => 'sharp', 'fork' => 'sharp', 'spoon' => 'round'), $list);
373+
374+
$list = DB::collection('items')->lists('name', '_id');
375+
$this->assertEquals(4, count($list));
376+
$this->assertEquals(24, strlen(key($list)));
373377
}
374378

375379
public function testAggregate()

0 commit comments

Comments
 (0)