Skip to content
Open
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
12 changes: 12 additions & 0 deletions src/MongoDB.Driver/Core/Misc/Feature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class Feature
private static readonly Feature __dateFromStringFormatArgument = new Feature("DateFromStringFormatArgument", WireVersion.Server40);
private static readonly Feature __dateOperatorsNewIn50 = new Feature("DateOperatorsNewIn50", WireVersion.Server50);
private static readonly Feature __densifyStage = new Feature("DensifyStage", WireVersion.Server51);
private static readonly Feature __deserializeEJsonOperator = new Feature("DeserializeEJsonOperator", WireVersion.Server83);
private static readonly Feature __documentsStage = new Feature("DocumentsStage", WireVersion.Server51);
private static readonly Feature __directConnectionSetting = new Feature("DirectConnectionSetting", WireVersion.Server44);
private static readonly Feature __electionIdPriorityInSDAM = new Feature("ElectionIdPriorityInSDAM ", WireVersion.Server60);
Expand Down Expand Up @@ -95,6 +96,7 @@ public class Feature
private static readonly Feature __scramSha256Authentication = new Feature("ScramSha256Authentication", WireVersion.Server40);
private static readonly Feature __serverReturnsResumableChangeStreamErrorLabel = new Feature("ServerReturnsResumableChangeStreamErrorLabel", WireVersion.Server44);
private static readonly Feature __serverReturnsRetryableWriteErrorLabel = new Feature("ServerReturnsRetryableWriteErrorLabel", WireVersion.Server44);
private static readonly Feature __serializeEJsonOperator = new Feature("SerializeEJsonOperator", WireVersion.Server83);
private static readonly Feature __setStage = new Feature("SetStage", WireVersion.Server42);
private static readonly Feature __setWindowFields = new Feature("SetWindowFields", WireVersion.Server50);
private static readonly Feature __setWindowFieldsLocf = new Feature("SetWindowFieldsLocf", WireVersion.Server52);
Expand Down Expand Up @@ -280,6 +282,11 @@ public class Feature
/// </summary>
public static Feature DensifyStage => __densifyStage;

/// <summary>
/// Gets the $deserializeEJSON operator feature.
/// </summary>
public static Feature DeserializeEJsonOperator => __deserializeEJsonOperator;

/// <summary>
/// Gets the documents stage feature.
/// </summary>
Expand Down Expand Up @@ -468,6 +475,11 @@ public class Feature
/// </summary>
public static Feature ServerReturnsRetryableWriteErrorLabel => __serverReturnsRetryableWriteErrorLabel;

/// <summary>
/// Gets the $serializeEJSON operator feature.
/// </summary>
public static Feature SerializeEJsonOperator => __serializeEJsonOperator;

/// <summary>
/// Gets the $set stage feature.
/// </summary>
Expand Down
55 changes: 55 additions & 0 deletions src/MongoDB.Driver/DeserializeEJsonOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace MongoDB.Driver
{
/// <summary>
/// Represents the options parameter for <see cref="Mql.DeserializeEJson{TInput, TOutput}(TInput, DeserializeEJsonOptions{TOutput})"/>.
/// </summary>
public abstract class DeserializeEJsonOptions
{
internal abstract bool OnErrorWasSet(out object onError);
}

/// <summary>
/// Represents the options parameter for <see cref="Mql.DeserializeEJson{TInput, TOutput}(TInput, DeserializeEJsonOptions{TOutput})"/>.
/// This class allows setting 'onError'.
/// </summary>
/// <typeparam name="TOutput">The type of 'onError'.</typeparam>
public class DeserializeEJsonOptions<TOutput> : DeserializeEJsonOptions
{
private TOutput _onError;
private bool _onErrorWasSet;

/// <summary>
/// The onError parameter.
/// </summary>
public TOutput OnError
{
get => _onError;
set
{
_onError = value;
_onErrorWasSet = true;
}
}

internal override bool OnErrorWasSet(out object onError)
{
onError = _onError;
return _onErrorWasSet;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ internal enum AstNodeType
DateTruncExpression,
DensifyStage,
DerivativeOrIntegralWindowExpression,
DeserializeEJsonExpression,
DocumentsStage,
ElemMatchFilterOperation,
ExistsFilterOperation,
Expand Down Expand Up @@ -130,6 +131,7 @@ internal enum AstNodeType
ReplaceWithStage,
RTrimExpression,
SampleStage,
SerializeEJsonExpression,
SetStage,
SetWindowFieldsStage,
ShiftWindowExpression,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Comment on lines +1 to +14
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

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

The Apache license header formatting here is inconsistent with other files in this directory (e.g., AstDateFromStringExpression.cs uses *-prefixed lines). Consider aligning the comment formatting to the standard header style used elsewhere to keep license headers uniform.

Suggested change
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

Copilot uses AI. Check for mistakes.

using MongoDB.Bson;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors;

namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
{
internal sealed class AstDeserializeEJsonExpression : AstExpression
{
private readonly AstExpression _input;
private readonly AstExpression _onError;

public AstDeserializeEJsonExpression(
AstExpression input,
AstExpression onError = null)
{
_input = Ensure.IsNotNull(input, nameof(input));
_onError = onError;
}

public AstExpression Input => _input;
public override AstNodeType NodeType => AstNodeType.DeserializeEJsonExpression;
public AstExpression OnError => _onError;

public override AstNode Accept(AstNodeVisitor visitor)
{
return visitor.VisitDeserializeEJsonExpression(this);
}

public override BsonValue Render()
{
return new BsonDocument
{
{ "$deserializeEJSON", new BsonDocument
{
{ "input", _input.Render() },
{ "onError", () => _onError.Render(), _onError != null }
}
}
};
}

public AstDeserializeEJsonExpression Update(
AstExpression input,
AstExpression onError)
{
if (input == _input && onError == _onError)
{
return this;
}

return new AstDeserializeEJsonExpression(input, onError);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ public static AstExpression DerivativeOrIntegralWindowExpression(AstDerivativeOr
return new AstDerivativeOrIntegralWindowExpression(@operator, arg, unit, window);
}

public static AstExpression DeserializeEJson(
AstExpression input,
AstExpression onError = null)
{
return new AstDeserializeEJsonExpression(input, onError);
}

public static AstExpression Divide(AstExpression arg1, AstExpression arg2)
{
if (arg1.IsConstant(out var constant1) && arg2.IsConstant(out var constant2))
Expand Down Expand Up @@ -743,6 +750,14 @@ public static AstExpression RTrim(AstExpression input, AstExpression chars = nul
return new AstRTrimExpression(input, chars);
}

public static AstExpression SerializeEJson(
AstExpression input,
AstExpression relaxed = null,
AstExpression onError = null)
{
return new AstSerializeEJsonExpression(input, relaxed, onError);
}

public static AstExpression SetDifference(AstExpression arg1, AstExpression arg2)
{
return new AstBinaryExpression(AstBinaryOperator.SetDifference, arg1, arg2);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Comment on lines +1 to +14
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

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

The Apache license header formatting here is inconsistent with other files in this directory (e.g., AstDateFromStringExpression.cs uses *-prefixed lines). Consider aligning the comment formatting to the standard header style used elsewhere to keep license headers uniform.

Suggested change
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

Choose a reason for hiding this comment

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

This seems to be incorrect.


using MongoDB.Bson;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors;

namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
{
internal sealed class AstSerializeEJsonExpression : AstExpression
{
private readonly AstExpression _input;
private readonly AstExpression _onError;
private readonly AstExpression _relaxed;

public AstSerializeEJsonExpression(
AstExpression input,
AstExpression relaxed = null,
AstExpression onError = null)
{
_input = Ensure.IsNotNull(input, nameof(input));
_relaxed = relaxed;
_onError = onError;
}

public AstExpression Input => _input;
public override AstNodeType NodeType => AstNodeType.SerializeEJsonExpression;
public AstExpression OnError => _onError;
public AstExpression Relaxed => _relaxed;

public override AstNode Accept(AstNodeVisitor visitor)
{
return visitor.VisitSerializeEJsonExpression(this);
}

public override BsonValue Render()
{
return new BsonDocument
{
{ "$serializeEJSON", new BsonDocument
{
{ "input", _input.Render() },
{ "relaxed", () => _relaxed.Render(), _relaxed != null },
{ "onError", () => _onError.Render(), _onError != null }
}
}
};
}

public AstSerializeEJsonExpression Update(
AstExpression input,
AstExpression relaxed,
AstExpression onError)
{
if (input == _input && relaxed == _relaxed && onError == _onError)
{
return this;
}

return new AstSerializeEJsonExpression(input, relaxed, onError);
}
}
}
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.

The new methods in this file break the alphabetical ordering.

Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ public virtual AstNode VisitDerivativeOrIntegralWindowExpression(AstDerivativeOr
return node.Update(node.Operator, VisitAndConvert(node.Arg), node.Unit, node.Window);
}

public virtual AstNode VisitDeserializeEJsonExpression(AstDeserializeEJsonExpression node)
{
return node.Update(VisitAndConvert(node.Input), VisitAndConvert(node.OnError));
}

public virtual AstNode VisitDocumentsStage(AstDocumentsStage node)
{
return node.Update(VisitAndConvert(node.Documents));
Expand Down Expand Up @@ -689,6 +694,11 @@ public virtual AstNode VisitSampleStage(AstSampleStage node)
return node;
}

public virtual AstNode VisitSerializeEJsonExpression(AstSerializeEJsonExpression node)
{
return node.Update(VisitAndConvert(node.Input), VisitAndConvert(node.Relaxed), VisitAndConvert(node.OnError));
}

public virtual AstNode VisitSetStage(AstSetStage node)
{
return node.Update(VisitAndConvert(node.Fields));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ internal static class MqlMethod
private static readonly MethodInfo __dateFromStringWithFormat;
private static readonly MethodInfo __dateFromStringWithFormatAndTimezone;
private static readonly MethodInfo __dateFromStringWithFormatAndTimezoneAndOnErrorAndOnNull;
private static readonly MethodInfo __deserializeEJson;
private static readonly MethodInfo __exists;
private static readonly MethodInfo __field;
private static readonly MethodInfo __isMissing;
private static readonly MethodInfo __isNullOrMissing;
private static readonly MethodInfo __serializeEJson;
private static readonly MethodInfo __sigmoid;
private static readonly MethodInfo __subtype;

Expand All @@ -55,10 +57,12 @@ static MqlMethod()
__dateFromStringWithFormat = ReflectionInfo.Method((string dateString, string format) => Mql.DateFromString(dateString, format));
__dateFromStringWithFormatAndTimezone = ReflectionInfo.Method((string dateString, string format, string timezone) => Mql.DateFromString(dateString, format, timezone));
__dateFromStringWithFormatAndTimezoneAndOnErrorAndOnNull = ReflectionInfo.Method((string dateString, string format, string timezone, DateTime? onError, DateTime? onNull) => Mql.DateFromString(dateString, format, timezone, onError, onNull));
__deserializeEJson = ReflectionInfo.Method((object value, DeserializeEJsonOptions<object> options) => Mql.DeserializeEJson(value, options));
__exists = ReflectionInfo.Method((object field) => Mql.Exists(field));
__field = ReflectionInfo.Method((object container, string fieldName, IBsonSerializer<object> serializer) => Mql.Field<object, object>(container, fieldName, serializer));
__isMissing = ReflectionInfo.Method((object field) => Mql.IsMissing(field));
__isNullOrMissing = ReflectionInfo.Method((object field) => Mql.IsNullOrMissing(field));
__serializeEJson = ReflectionInfo.Method((object value, SerializeEJsonOptions<object> options) => Mql.SerializeEJson(value, options));
__sigmoid = ReflectionInfo.Method((double value) => Mql.Sigmoid(value));
__subtype = ReflectionInfo.Method((object value) => Mql.Subtype(value));

Expand Down Expand Up @@ -110,10 +114,12 @@ static MqlMethod()
public static MethodInfo DateFromStringWithFormat => __dateFromStringWithFormat;
public static MethodInfo DateFromStringWithFormatAndTimezone => __dateFromStringWithFormatAndTimezone;
public static MethodInfo DateFromStringWithFormatAndTimezoneAndOnErrorAndOnNull => __dateFromStringWithFormatAndTimezoneAndOnErrorAndOnNull;
public static MethodInfo DeserializeEJson => __deserializeEJson;
public static MethodInfo Exists => __exists;
public static MethodInfo Field => __field;
public static MethodInfo IsMissing => __isMissing;
public static MethodInfo IsNullOrMissing => __isNullOrMissing;
public static MethodInfo SerializeEJson => __serializeEJson;
public static MethodInfo Sigmoid => __sigmoid;
public static MethodInfo Subtype => __subtype;

Expand Down
Loading