Skip to content

CSHARP-5656: Support Aggregation Operator to generate random object ids#1931

Open
sanych-sun wants to merge 5 commits intomongodb:mainfrom
sanych-sun:csharp5656
Open

CSHARP-5656: Support Aggregation Operator to generate random object ids#1931
sanych-sun wants to merge 5 commits intomongodb:mainfrom
sanych-sun:csharp5656

Conversation

@sanych-sun
Copy link
Copy Markdown
Member

@sanych-sun sanych-sun commented Mar 30, 2026

No description provided.

@sanych-sun sanych-sun requested a review from a team as a code owner March 30, 2026 21:27
Copilot AI review requested due to automatic review settings March 30, 2026 21:27
@sanych-sun sanych-sun added the feature Adds new user-facing functionality. label Mar 30, 2026
@sanych-sun sanych-sun requested review from BorisDog, adelinowona and kyra-rk and removed request for kyra-rk March 30, 2026 21:27
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds LINQ3 translation support for ObjectId.GenerateNewId() (parameterless overload) by translating it to the aggregation expression $createObjectId, rather than partially evaluating it client-side to a constant.

Changes:

  • Introduces a new AST expression node for $createObjectId and wires it into the AST visitor/rendering surface.
  • Adds a dedicated method translator + serializer deduction support for ObjectId.GenerateNewId() and registers it in the method-call translation pipeline.
  • Prevents partial evaluation of ObjectId.GenerateNewId() and adds unit + integration test coverage guarded by a new server feature flag.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/GenerateNewIdMethodToAggregationExpressionTranslatorTests.cs Unit tests validating $createObjectId AST output and unsupported overload behavior.
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/SerializerFinders/ObjectIdCreateNewIdTests.cs SerializerFinder test ensuring ObjectId.GenerateNewId() resolves to ObjectIdSerializer.
tests/MongoDB.Driver.Tests/Linq/Integration/ObjectIdGenerateNewIdTests.cs Integration coverage for LINQ/queryable + builders + pipeline translation/execution with $createObjectId.
src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/GenerateNewIdMethodToAggregationExpressionTranslator.cs New translator mapping ObjectId.GenerateNewId() to AstExpression.CreateObjectId().
src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodCallExpressionToAggregationExpressionTranslator.cs Registers GenerateNewId method name dispatch to the new translator.
src/MongoDB.Driver/Linq/Linq3Implementation/SerializerFinders/SerializerFinderVisitMethodCall.cs Adds serializer deduction for ObjectId.GenerateNewId() calls.
src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/ReflectionInfo.cs Adds parameterless Method<TResult>(Expression<Func<TResult>>) helper for reflection extraction.
src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/ObjectIdMethod.cs Centralizes reflection info for the parameterless ObjectId.GenerateNewId() overload.
src/MongoDB.Driver/Linq/Linq3Implementation/Misc/PartialEvaluator.cs Prevents partial evaluation of ObjectId.GenerateNewId() to avoid constant folding.
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Visitors/AstNodeVisitor.cs Adds a visitor hook for the new $createObjectId AST node.
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstExpression.cs Adds AstExpression.CreateObjectId() factory method.
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstCreateObjectIdExpression.cs New AST node rendering { $createObjectId: {} }.
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/AstNodeType.cs Adds CreateObjectIdExpression node type.
src/MongoDB.Driver/Core/Misc/Feature.cs Adds Feature.CreateObjectIdExpression for server capability gating.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +24 to +27
public class ObjectIdCreateNewIdTests
{
[Fact]
public void SerializerFinder_should_resolve_objectId_createNewId()
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

The test file/class/method names use CreateNewId/createNewId, but the expression under test is ObjectId.GenerateNewId() and it translates to $createObjectId. Renaming the test class (and test method name) to reference GenerateNewId (or $createObjectId) would reduce confusion and improve discoverability.

Suggested change
public class ObjectIdCreateNewIdTests
{
[Fact]
public void SerializerFinder_should_resolve_objectId_createNewId()
public class ObjectIdGenerateNewIdTests
{
[Fact]
public void SerializerFinder_should_resolve_objectId_generateNewId()

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed


namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
{
internal sealed class AstCreateObjectIdExpression : AstExpression
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

minor, for your consideration: file scoped namespaces, and expression body methods.

I think we should write the new code with new style and not worry about mixed styles.

@sanych-sun sanych-sun requested a review from BorisDog April 9, 2026 01:02
Comment on lines +25 to +36
public override AstNode Accept(AstNodeVisitor visitor)
{
return visitor.VisitCreateObjectIdExpression(this);
}

public override BsonValue Render()
{
return new BsonDocument
{
{ "$createObjectId", new BsonDocument() }
};
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Could use expression bodies instead of block bodies

using MongoDB.Driver.Linq.Linq3Implementation.Misc;
using MongoDB.Driver.Linq.Linq3Implementation.Reflection;

namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

use file scoped namespace?

}

[Fact]
public void MqlCreateObjectId_in_where()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we continue adding the "expected behavior" part in tests names here as well?

return ExtractConstructorInfoFromLambda(lambda);
}

public static MethodInfo Method<TResult>(Expression<Func<TResult>> lambda)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

expression body?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Adds new user-facing functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants