Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit 985f78f

Browse files
committed
Add enum for $meta operator and add tests
1 parent 7b15fe0 commit 985f78f

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

generator/config/expression/meta.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,29 @@ arguments:
1111
name: keyword
1212
type:
1313
- string
14+
tests:
15+
-
16+
name: 'textScore'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/#-meta---textscore-'
18+
pipeline:
19+
-
20+
$match:
21+
$text:
22+
$search: 'cake'
23+
-
24+
$group:
25+
_id:
26+
$meta: 'textScore'
27+
count:
28+
$sum: 1
29+
-
30+
name: 'indexKey'
31+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/#-meta---indexkey-'
32+
pipeline:
33+
-
34+
$match:
35+
type: 'apparel'
36+
-
37+
$addFields:
38+
idxKey:
39+
$meta: 'indexKey'

src/Builder/Meta.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Builder;
6+
7+
use MongoDB\Builder\Type\Encode;
8+
use MongoDB\Builder\Type\ExpressionInterface;
9+
use MongoDB\Builder\Type\OperatorInterface;
10+
11+
/**
12+
* Returns the metadata associated with a document, e.g. "textScore" when performing text search.
13+
*
14+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/
15+
*/
16+
enum Meta: string implements OperatorInterface, ExpressionInterface
17+
{
18+
public const ENCODE = Encode::Single;
19+
20+
/**
21+
* Returns the score associated with the corresponding $text query for each
22+
* matching document. The text score signifies how well the document matched
23+
* the search term or terms.
24+
*/
25+
case TextScore = 'textScore';
26+
27+
/**
28+
* Returns an index key for the document if a non-text index is used. The
29+
* { $meta: "indexKey" } expression is for debugging purposes only, and
30+
* not for application logic, and is preferred over cursor.returnKey().
31+
*/
32+
case IndexKey = 'indexKey';
33+
34+
public function getOperator(): string
35+
{
36+
return '$meta';
37+
}
38+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Expression;
6+
7+
use MongoDB\Builder\Accumulator;
8+
use MongoDB\Builder\Expression;
9+
use MongoDB\Builder\Meta;
10+
use MongoDB\Builder\Pipeline;
11+
use MongoDB\Builder\Query;
12+
use MongoDB\Builder\Stage;
13+
use MongoDB\Tests\Builder\PipelineTestCase;
14+
15+
/**
16+
* Test $meta expression
17+
*/
18+
class MetaOperatorTest extends PipelineTestCase
19+
{
20+
public function testIndexKey(): void
21+
{
22+
$pipeline = new Pipeline(
23+
Stage::match(
24+
type: 'apparel',
25+
),
26+
Stage::addFields(
27+
idxKey: Expression::meta('indexKey'),
28+
),
29+
);
30+
31+
$this->assertSamePipeline(Pipelines::MetaIndexKey, $pipeline);
32+
}
33+
34+
public function testIndexKeyWithEnum(): void
35+
{
36+
$pipeline = new Pipeline(
37+
Stage::match(
38+
type: 'apparel',
39+
),
40+
Stage::addFields(
41+
idxKey: Meta::IndexKey,
42+
),
43+
);
44+
45+
$this->assertSamePipeline(Pipelines::MetaIndexKey, $pipeline);
46+
}
47+
48+
public function testTextScore(): void
49+
{
50+
$pipeline = new Pipeline(
51+
Stage::match(
52+
Query::text('cake'),
53+
),
54+
Stage::group(
55+
_id: Expression::meta('textScore'),
56+
count: Accumulator::sum(1),
57+
),
58+
);
59+
60+
$this->assertSamePipeline(Pipelines::MetaTextScore, $pipeline);
61+
}
62+
63+
public function testTextScoreWithEnum(): void
64+
{
65+
$pipeline = new Pipeline(
66+
Stage::match(
67+
Query::text('cake'),
68+
),
69+
Stage::group(
70+
_id: Meta::TextScore,
71+
count: Accumulator::sum(1),
72+
),
73+
);
74+
75+
$this->assertSamePipeline(Pipelines::MetaTextScore, $pipeline);
76+
}
77+
}

tests/Builder/Expression/Pipelines.php

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)