Skip to content

Commit

Permalink
Merge pull request #88 from alcaeus/distinct-in-query
Browse files Browse the repository at this point in the history
Convert legacy types in query for distinct calls
  • Loading branch information
alcaeus committed Mar 31, 2016
2 parents 09017b3 + e95c899 commit 9e70cdb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG-1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ milestone.
* [#85](https://github.com/alcaeus/mongo-php-adapter/pull/85) fixes calls to
`MongoCollection::count` using the legacy syntax of providing `skip` and `limit`
arguments instead of an `options` array.
* [#88](https://github.com/alcaeus/mongo-php-adapter/pull/88) fixes an error
where a call to `MongoCollection::distinct` with a query did not convert legacy
BSON types to the new driver types.


1.0.0 (2016-03-18)
Expand Down
2 changes: 1 addition & 1 deletion lib/Mongo/MongoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public function find(array $query = [], array $fields = [])
public function distinct($key, array $query = [])
{
try {
return array_map([TypeConverter::class, 'toLegacy'], $this->collection->distinct($key, $query));
return array_map([TypeConverter::class, 'toLegacy'], $this->collection->distinct($key, TypeConverter::fromLegacy($query)));
} catch (\MongoDB\Driver\Exception\Exception $e) {
return false;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,32 @@ public function testDistinctWithQuery()
$this->assertEquals(['bar'], $values);
}

public function testDistinctWithIdQuery()
{
$document1 = ['foo' => 'bar'];
$document2 = ['foo' => 'bar'];
$document3 = ['foo' => 'foo'];

$collection = $this->getCollection();
$collection->insert($document1);
$collection->insert($document2);
$collection->insert($document3);

$this->assertSame(
['bar'],
$collection->distinct('foo', ['_id' => [
'$in' => [$document1['_id'], $document2['_id']]
]])
);

$this->assertEquals(
['bar', 'foo'],
$collection->distinct('foo', ['_id' => [
'$in' => [$document1['_id'], $document3['_id']]
]])
);
}

public function testAggregate()
{
$collection = $this->getCollection();
Expand Down

0 comments on commit 9e70cdb

Please sign in to comment.