Skip to content
This repository was archived by the owner on Nov 11, 2020. It is now read-only.
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
5 changes: 5 additions & 0 deletions CHANGELOG-1.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ CHANGELOG for 1.6.x
This changelog references the relevant changes (bug and security fixes) done
in 1.6.x patch versions.

1.6.4 (2019-07-10)
------------------

* [#333](https://github.com/doctrine/mongodb/pull/333): Fix wrong syntax for replaceRoot stage

1.6.3 (2018-07-20)
------------------

Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/MongoDB/Aggregation/Stage/ReplaceRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\MongoDB\Aggregation\Builder;
use Doctrine\MongoDB\Aggregation\Expr;
use function is_array;

/**
* Fluent interface for adding a $replaceRoot stage to an aggregation pipeline.
Expand Down Expand Up @@ -35,8 +36,12 @@ public function __construct(Builder $builder, $expression = null)
*/
public function getExpression()
{
$expression = $this->expression !== null ? $this->convertExpression($this->expression) : $this->expr->getExpression();

return [
'$replaceRoot' => $this->expression !== null ? $this->convertExpression($this->expression) : $this->expr->getExpression()
'$replaceRoot' => [
'newRoot' => is_array($expression) ? (object) $expression : $expression,
],
];
}

Expand Down
35 changes: 32 additions & 3 deletions tests/Doctrine/MongoDB/Tests/Aggregation/Stage/ReplaceRootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ public function testReplaceRootStage()
->field('product')
->multiply('$field', 5);

$this->assertSame(['$replaceRoot' => ['product' => ['$multiply' => ['$field', 5]]]], $replaceRootStage->getExpression());
$this->assertEquals(
[
'$replaceRoot' => [
'newRoot' => (object) [
'product' => ['$multiply' => ['$field', 5]],
],
],
],
$replaceRootStage->getExpression()
);
}

public function testReplaceRootFromBuilder()
Expand All @@ -28,14 +37,34 @@ public function testReplaceRootFromBuilder()
->field('product')
->multiply('$field', 5);

$this->assertSame([['$replaceRoot' => ['product' => ['$multiply' => ['$field', 5]]]]], $builder->getPipeline());
$this->assertEquals(
[
[
'$replaceRoot' => [
'newRoot' => (object) [
'product' => ['$multiply' => ['$field', 5]],
],
],
],
],
$builder->getPipeline()
);
}

public function testReplaceWithEmbeddedDocument()
{
$builder = $this->getTestAggregationBuilder();
$builder->replaceRoot('$some.embedded.document');

$this->assertSame([['$replaceRoot' => '$some.embedded.document']], $builder->getPipeline());
$this->assertEquals(
[
[
'$replaceRoot' => [
'newRoot' => '$some.embedded.document'
]
]
],
$builder->getPipeline()
);
}
}