Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert legacy types in query for distinct calls #88

Merged
merged 1 commit into from
Mar 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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