Skip to content
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
6 changes: 5 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Stage/ReplaceRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ public function __construct(Builder $builder, DocumentManager $documentManager,
*/
public function getExpression() : array
{
$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
8 changes: 6 additions & 2 deletions tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ public function testPipelineConvertsTypes()
],
[
'$replaceRoot' => [
'isToday' => ['$eq' => ['$createdAt', new UTCDateTime((int) $dateTime->format('Uv'))]],
'newRoot' => (object) [
'isToday' => [
'$eq' => ['$createdAt', new UTCDateTime((int) $dateTime->format('Uv'))],
],
],
],
],
];
Expand Down Expand Up @@ -265,7 +269,7 @@ public function testFieldNameConversion()
[
'$sort' => ['ip' => 1],
],
['$replaceRoot' => '$ip'],
['$replaceRoot' => ['newRoot' => '$ip']],
];

$this->assertEquals($expectedPipeline, $builder->getPipeline());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public function testTypeConversion()
$this->assertEquals(
[
'$replaceRoot' => [
'isToday' => ['$eq' => ['$createdAt', $mongoDate]],
'newRoot' => (object) [
'isToday' => ['$eq' => ['$createdAt', $mongoDate]],
],
],
],
$stage->getExpression()
Expand All @@ -49,7 +51,9 @@ public function testTypeConversionWithDirectExpression()
$this->assertEquals(
[
'$replaceRoot' => [
'isToday' => ['$eq' => ['$createdAt', $mongoDate]],
'newRoot' => (object) [
'isToday' => ['$eq' => ['$createdAt', $mongoDate]],
],
],
],
$stage->getExpression()
Expand All @@ -68,7 +72,9 @@ public function testFieldNameConversion()
$this->assertEquals(
[
'$replaceRoot' => [
'someField' => ['$concat' => ['$ip', 'foo']],
'newRoot' => (object) [
'someField' => ['$concat' => ['$ip', 'foo']],
],
],
],
$stage->getExpression()
Expand All @@ -83,7 +89,9 @@ public function testFieldNameConversionWithDirectExpression()
->replaceRoot('$authorIp');

$this->assertEquals(
['$replaceRoot' => '$ip'],
[
'$replaceRoot' => ['newRoot' => '$ip'],
],
$stage->getExpression()
);
}
Expand Down