Skip to content

Commit

Permalink
Add enum for $meta operator and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jan 22, 2024
1 parent 7b15fe0 commit 985f78f
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
26 changes: 26 additions & 0 deletions generator/config/expression/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,29 @@ arguments:
name: keyword
type:
- string
tests:
-
name: 'textScore'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/#-meta---textscore-'
pipeline:
-
$match:
$text:
$search: 'cake'
-
$group:
_id:
$meta: 'textScore'
count:
$sum: 1
-
name: 'indexKey'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/#-meta---indexkey-'
pipeline:
-
$match:
type: 'apparel'
-
$addFields:
idxKey:
$meta: 'indexKey'
38 changes: 38 additions & 0 deletions src/Builder/Meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace MongoDB\Builder;

use MongoDB\Builder\Type\Encode;
use MongoDB\Builder\Type\ExpressionInterface;
use MongoDB\Builder\Type\OperatorInterface;

/**
* Returns the metadata associated with a document, e.g. "textScore" when performing text search.
*
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/
*/
enum Meta: string implements OperatorInterface, ExpressionInterface
{
public const ENCODE = Encode::Single;

/**
* Returns the score associated with the corresponding $text query for each
* matching document. The text score signifies how well the document matched
* the search term or terms.
*/
case TextScore = 'textScore';

/**
* Returns an index key for the document if a non-text index is used. The
* { $meta: "indexKey" } expression is for debugging purposes only, and
* not for application logic, and is preferred over cursor.returnKey().
*/
case IndexKey = 'indexKey';

public function getOperator(): string
{
return '$meta';
}
}
77 changes: 77 additions & 0 deletions tests/Builder/Expression/MetaOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Expression;

use MongoDB\Builder\Accumulator;
use MongoDB\Builder\Expression;
use MongoDB\Builder\Meta;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Query;
use MongoDB\Builder\Stage;
use MongoDB\Tests\Builder\PipelineTestCase;

/**
* Test $meta expression
*/
class MetaOperatorTest extends PipelineTestCase
{
public function testIndexKey(): void
{
$pipeline = new Pipeline(
Stage::match(
type: 'apparel',
),
Stage::addFields(
idxKey: Expression::meta('indexKey'),
),
);

$this->assertSamePipeline(Pipelines::MetaIndexKey, $pipeline);
}

public function testIndexKeyWithEnum(): void
{
$pipeline = new Pipeline(
Stage::match(
type: 'apparel',
),
Stage::addFields(
idxKey: Meta::IndexKey,
),
);

$this->assertSamePipeline(Pipelines::MetaIndexKey, $pipeline);
}

public function testTextScore(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::text('cake'),
),
Stage::group(
_id: Expression::meta('textScore'),
count: Accumulator::sum(1),
),
);

$this->assertSamePipeline(Pipelines::MetaTextScore, $pipeline);
}

public function testTextScoreWithEnum(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::text('cake'),
),
Stage::group(
_id: Meta::TextScore,
count: Accumulator::sum(1),
),
);

$this->assertSamePipeline(Pipelines::MetaTextScore, $pipeline);
}
}
51 changes: 51 additions & 0 deletions tests/Builder/Expression/Pipelines.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 985f78f

Please sign in to comment.