diff --git a/src/MongoDB.Driver/Core/Misc/Feature.cs b/src/MongoDB.Driver/Core/Misc/Feature.cs
index a201eec9cbf..666b0fa46e6 100644
--- a/src/MongoDB.Driver/Core/Misc/Feature.cs
+++ b/src/MongoDB.Driver/Core/Misc/Feature.cs
@@ -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);
@@ -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);
@@ -280,6 +282,11 @@ public class Feature
///
public static Feature DensifyStage => __densifyStage;
+ ///
+ /// Gets the $deserializeEJSON operator feature.
+ ///
+ public static Feature DeserializeEJsonOperator => __deserializeEJsonOperator;
+
///
/// Gets the documents stage feature.
///
@@ -468,6 +475,11 @@ public class Feature
///
public static Feature ServerReturnsRetryableWriteErrorLabel => __serverReturnsRetryableWriteErrorLabel;
+ ///
+ /// Gets the $serializeEJSON operator feature.
+ ///
+ public static Feature SerializeEJsonOperator => __serializeEJsonOperator;
+
///
/// Gets the $set stage feature.
///
diff --git a/src/MongoDB.Driver/DeserializeEJsonOptions.cs b/src/MongoDB.Driver/DeserializeEJsonOptions.cs
new file mode 100644
index 00000000000..e989d4afce4
--- /dev/null
+++ b/src/MongoDB.Driver/DeserializeEJsonOptions.cs
@@ -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
+{
+ ///
+ /// Represents the options parameter for .
+ ///
+ public abstract class DeserializeEJsonOptions
+ {
+ internal abstract bool OnErrorWasSet(out object onError);
+ }
+
+ ///
+ /// Represents the options parameter for .
+ /// This class allows setting 'onError'.
+ ///
+ /// The type of 'onError'.
+ public class DeserializeEJsonOptions : DeserializeEJsonOptions
+ {
+ private TOutput _onError;
+ private bool _onErrorWasSet;
+
+ ///
+ /// The onError parameter.
+ ///
+ public TOutput OnError
+ {
+ get => _onError;
+ set
+ {
+ _onError = value;
+ _onErrorWasSet = true;
+ }
+ }
+
+ internal override bool OnErrorWasSet(out object onError)
+ {
+ onError = _onError;
+ return _onErrorWasSet;
+ }
+ }
+}
diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/AstNodeType.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/AstNodeType.cs
index ace59a917e0..0e2cc6e6d55 100644
--- a/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/AstNodeType.cs
+++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/AstNodeType.cs
@@ -52,6 +52,7 @@ internal enum AstNodeType
DateTruncExpression,
DensifyStage,
DerivativeOrIntegralWindowExpression,
+ DeserializeEJsonExpression,
DocumentsStage,
ElemMatchFilterOperation,
ExistsFilterOperation,
@@ -130,6 +131,7 @@ internal enum AstNodeType
ReplaceWithStage,
RTrimExpression,
SampleStage,
+ SerializeEJsonExpression,
SetStage,
SetWindowFieldsStage,
ShiftWindowExpression,
diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstDeserializeEJsonExpression.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstDeserializeEJsonExpression.cs
new file mode 100644
index 00000000000..deef39e418d
--- /dev/null
+++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstDeserializeEJsonExpression.cs
@@ -0,0 +1,66 @@
+/* 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.
+*/
+
+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) =>
+ 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);
+ }
+}
diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstExpression.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstExpression.cs
index 54462a0aa4d..7291d46d57a 100644
--- a/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstExpression.cs
+++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstExpression.cs
@@ -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))
@@ -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);
diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstSerializeEJsonExpression.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstSerializeEJsonExpression.cs
new file mode 100644
index 00000000000..c058ae11f8e
--- /dev/null
+++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstSerializeEJsonExpression.cs
@@ -0,0 +1,72 @@
+/* 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.
+*/
+
+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) =>
+ 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);
+ }
+}
diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Visitors/AstNodeVisitor.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Visitors/AstNodeVisitor.cs
index 733cdb8c7bd..768acb2c9f5 100644
--- a/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Visitors/AstNodeVisitor.cs
+++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Visitors/AstNodeVisitor.cs
@@ -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));
@@ -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));
diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/MqlMethod.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/MqlMethod.cs
index 3a935cd8908..2e691b6ea34 100644
--- a/src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/MqlMethod.cs
+++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/MqlMethod.cs
@@ -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;
@@ -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