Skip to content

Commit ccca05c

Browse files
authored
CSHARP-5949: $convert should allow any type to be converted to string (#1940)
1 parent f845b6f commit ccca05c

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/MongoDB.Driver/Core/Misc/Feature.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class Feature
4444
private static readonly Feature __clientBulkWrite = new Feature("ClientBulkWrite", WireVersion.Server80);
4545
private static readonly Feature __clientSideEncryption = new Feature("ClientSideEncryption", WireVersion.Server42);
4646
private static readonly Feature __clusteredIndexes = new Feature("ClusteredIndexes", WireVersion.Server53);
47+
private static readonly Feature __convertOperatorAnyToString = new Feature("ConvertOperatorAnyToString", WireVersion.Server83);
4748
private static readonly Feature __convertOperatorBinDataToFromNumeric = new Feature("ConvertOperatorBinDataToFromNumeric", WireVersion.Server81);
4849
private static readonly Feature __convertOperatorBinDataToFromString= new Feature("ConvertOperatorBinDataToFromString", WireVersion.Server80);
4950
private static readonly Feature __convertOperatorStringToObjectOrArray = new Feature("ConvertOperatorStringToObjectOrArray", WireVersion.Server83);
@@ -203,6 +204,11 @@ public class Feature
203204
/// </summary>
204205
public static Feature ClusteredIndexes => __clusteredIndexes;
205206

207+
/// <summary>
208+
/// Gets the conversion of any type to string feature.
209+
/// </summary>
210+
public static Feature ConvertOperatorAnyToString => __convertOperatorAnyToString;
211+
206212
/// <summary>
207213
/// Gets the conversion of binary data to/from numeric types feature.
208214
/// </summary>

tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/ConvertMethodToAggregationExpressionTranslatorTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,28 @@ public void Convert_should_work(
500500
AssertOutcome(collection, queryable, expectedStages, expectedValue);
501501
}
502502

503+
[Theory]
504+
[InlineData(30, """{"a":1,"b":"hello"}""")]
505+
[InlineData(31, """[1,"two",3]""")]
506+
public void Convert_to_string_from_any_type_should_work(int id, string expectedResult)
507+
{
508+
RequireServer.Check().Supports(Feature.ConvertOperatorAnyToString);
509+
510+
var collection = Fixture.Collection;
511+
var queryable = collection.AsQueryable()
512+
.Where(x => x.Id == id)
513+
.Select(x => Mql.Convert<BsonValue, string>(x.BsonValueProperty, null));
514+
515+
var expectedStages =
516+
new[]
517+
{
518+
$"{{ $match : {{ _id : {id} }} }}",
519+
"{ $project: { _v : { $toString : '$BsonValueProperty' }, _id : 0 } }",
520+
};
521+
522+
AssertOutcome(collection, queryable, expectedStages, expectedResult);
523+
}
524+
503525
[Fact]
504526
public void Convert_to_BsonDocument_should_work()
505527
{
@@ -656,6 +678,8 @@ public sealed class ClassFixture : MongoCollectionFixture<TestClass, BsonDocumen
656678
BsonDocument.Parse("{ _id : 22, IntProperty: 33 }"),
657679
BsonDocument.Parse("{ _id : 23, StringProperty: '2018-03-03' }"),
658680
BsonDocument.Parse("{ _id : 24, StringProperty: '5ab9cbfa31c2ab715d42129e' }"),
681+
BsonDocument.Parse("{ _id : 30, BsonValueProperty: { a: 1, b: 'hello' } }"),
682+
BsonDocument.Parse("{ _id : 31, BsonValueProperty: [1, 'two', 3] }"),
659683
BsonDocument.Parse("{ _id : 40, StringProperty: '{\"a\": 1, \"b\": \"hello\"}' }"),
660684
BsonDocument.Parse("{ _id : 41, StringProperty: '[1, 2, 3]' }"),
661685
];
@@ -665,6 +689,7 @@ public class TestClass
665689
{
666690
public int Id { get; set; }
667691
public BsonBinaryData BinaryProperty { get; set; }
692+
public BsonValue BsonValueProperty { get; set; }
668693
public double DoubleProperty { get; set; }
669694
public int IntProperty { get; set; }
670695
public long LongProperty { get; set; }

0 commit comments

Comments
 (0)