Skip to content

Commit 218ccbd

Browse files
committed
added support for $literal when a constant string begins with a dollar sign.
1 parent 71fa8d6 commit 218ccbd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/MongoDB.Driver.Tests/Linq/Translators/AggregateProjectionTranslatorTests_Project.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,16 @@ public async Task Should_translate_less_than_or_equal()
288288
result.Value.Result.Should().BeFalse();
289289
}
290290

291+
[Test]
292+
public async Task Should_translate_literal_when_a_constant_strings_begins_with_a_dollar()
293+
{
294+
var result = await Project(x => new { Result = x.A == "$1" });
295+
296+
result.Projection.Should().Be("{ Result: { \"$eq\": [\"$A\", { \"$literal\": \"$1\" }] }, _id: 0 }");
297+
298+
result.Value.Result.Should().BeFalse();
299+
}
300+
291301
[Test]
292302
public async Task Should_translate_map_with_value()
293303
{

src/MongoDB.Driver/Linq/Translators/AggregateProjectionTranslator.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,15 @@ private BsonValue BuildValue(Expression node)
109109
case ExpressionType.Conditional:
110110
return BuildConditional((ConditionalExpression)node);
111111
case ExpressionType.Constant:
112-
return BsonValue.Create(((ConstantExpression)node).Value);
112+
var value = BsonValue.Create(((ConstantExpression)node).Value);
113+
var stringValue = value as BsonString;
114+
if (stringValue != null && stringValue.Value.StartsWith("$"))
115+
{
116+
value = new BsonDocument("$literal", value);
117+
}
118+
// TODO: there may be other instances where we should use a literal...
119+
// but I can't think of any yet.
120+
return value;
113121
case ExpressionType.Convert:
114122
case ExpressionType.ConvertChecked:
115123
return BuildValue(((UnaryExpression)node).Operand);

0 commit comments

Comments
 (0)