Skip to content

PHPLIB-1695 Add $createObjectId operator to the agg builder #1738

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

Merged
merged 1 commit into from
Aug 1, 2025
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
17 changes: 17 additions & 0 deletions generator/config/expression/createObjectId.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# $schema: ../schema.json
name: $createObjectId
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/createObjectId/'
type:
- resolvesToObjectId
encode: object
description: |
Returns a random object ID
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/createObjectId/#example'
pipeline:
-
$project:
objectId:
$createObjectId: {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the server source:

$createObjectId only accepts the empty object as argument

28 changes: 28 additions & 0 deletions src/Builder/Expression/CreateObjectIdOperator.php

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

10 changes: 10 additions & 0 deletions src/Builder/Expression/FactoryTrait.php

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

27 changes: 27 additions & 0 deletions tests/Builder/Expression/CreateObjectIdOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Expression;

use MongoDB\Builder\Expression;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Stage;
use MongoDB\Tests\Builder\PipelineTestCase;

/**
* Test $createObjectId expression
*/
class CreateObjectIdOperatorTest extends PipelineTestCase
{
public function testExample(): void
{
$pipeline = new Pipeline(
Stage::project(
objectId: Expression::createObjectId(),
),
);

$this->assertSamePipeline(Pipelines::CreateObjectIdExample, $pipeline);
}
}
17 changes: 17 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.