[CSHARP-4519] UpdateDefinition can't handle casts when combined (multiple Set) Created: 06/Feb/23  Updated: 09/Feb/23  Resolved: 09/Feb/23

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: 2.18.0, 2.19.0
Fix Version/s: None

Type: Bug Priority: Unknown
Reporter: Alexandre Junior Assignee: Robert Stam
Resolution: Duplicate Votes: 0
Labels: LINQ3
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-4499 Support Convert calls to a base type ... Closed
Related
is related to CSHARP-4428 LINQ3 not handling down cast in Updat... Closed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

UpdateDefinition can't handle casts when combined (calling multiple Set).

Driver version: 2.19.0

Repro

 

using System.Linq.Expressions;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
var mongoUrl = MongoUrl.Create("mongodb://admin:123456@localhost/management_reports?authMechanism=SCRAM-SHA-1;authSource=admin;minPoolSize=1;retryWrites=true");
var clientSettings = MongoClientSettings.FromUrl(mongoUrl);
clientSettings.LinqProvider = LinqProvider.V3;
var client = new MongoClient(clientSettings);
var db = client.GetDatabase("test");
var carCollection = db.GetCollection<Car>("cars");
var updateDef = GetUpdateFieldDefinition((t => t.Type, "super-fast"), (t => t.Price, 100m));
await carCollection.UpdateManyAsync(t => t.Type == "fast", updateDef);
Console.ReadKey();
static UpdateDefinition<Car> GetUpdateFieldDefinition(params (Expression<Func<Car, object>> fieldSelector, object value)[] fieldsToUpdate)
{
    UpdateDefinition<Car> updateDefinition = null;
    foreach (var (fieldSelector, value) in fieldsToUpdate)
    {
        updateDefinition = updateDefinition?.Set(fieldSelector, value) ?? Builders<Car>.Update.Set(fieldSelector, value);
    }
    return updateDefinition;
}
public class Car
{
    public string Type { get; set; }
    public decimal Price { get; set; }
}

 



 Comments   
Comment by Robert Stam [ 09/Feb/23 ]

I was able to reproduce this and also to confirm that this is caused by the same underlying issue that is fixed by CSHARP-4499.

I'm closing this as a duplicate even though the reported scenario is different because it shares the same underlying cause.

Thanks again for reporting this and also for providing good clear instructions on how to reproduce.

Comment by Robert Stam [ 09/Feb/23 ]

Thank you for reporting this issue. I will begin investigating it soon.

Comment by Alexandre Junior [ 06/Feb/23 ]

Error: Expression not supported: Convert(t.Price, Object).

Stack Trace:

at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.ConvertExpressionToFilterFieldTranslator.Translate(TranslationContext context, UnaryExpression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.LinqProviderAdapterV3.TranslateExpressionToField[TDocument,TField](Expression`1 expression, IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, Boolean allowScalarValueForArrayField)
   at MongoDB.Driver.ExpressionFieldDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
   at MongoDB.Driver.OperatorUpdateDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
   at MongoDB.Driver.CombinedUpdateDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
   at MongoDB.Driver.MongoCollectionImpl`1.ConvertWriteModelToWriteRequest(WriteModel`1 model, Int32 index)
   at System.Linq.Enumerable.<SelectIterator>d__199`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation..ctor(CollectionNamespace collectionNamespace, IEnumerable`1 requests, MessageEncoderSettings messageEncoderSettings)
   at MongoDB.Driver.MongoCollectionImpl`1.CreateBulkWriteOperation(IClientSessionHandle session, IEnumerable`1 requests, BulkWriteOptions options)
   at MongoDB.Driver.MongoCollectionImpl`1.<BulkWriteAsync>d__31.MoveNext()
   at MongoDB.Driver.MongoCollectionImpl`1.<UsingImplicitSessionAsync>d__107`1.MoveNext()
   at MongoDB.Driver.MongoCollectionBase`1.<UpdateManyAsync>d__101.MoveNext()
   at Program.<<Main>$>d__0.MoveNext() in C:\Users\alexandre.murari\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 15

 

Generated at Wed Feb 07 21:48:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.