[CSHARP-4501] Updating from version 2.18 to 2.19 of the C# MongoDB driver is breaking code to update documents in the database across a large codebase. Created: 30/Jan/23  Updated: 30/Jan/23  Resolved: 30/Jan/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: J.D. Robertson Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-4502 UpdateMany call fails after updating ... Closed

 Description   

Summary

Please provide a clear and concise description of the bug.

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

How to Reproduce

Steps to reproduce. If possible, please include a Short, Self Contained, Correct (Compilable), Example.

Additional Background

Please provide any additional background information that may be helpful in diagnosing the bug.



 Comments   
Comment by J.D. Robertson [ 30/Jan/23 ]

I can't seem to update or close this issue so I created a new one with a proper description. Please close this.

Comment by J.D. Robertson [ 30/Jan/23 ]

Apologies. I am unfamiliar with this bug tracking tool and an attempt to add a newline created the bug report before I was ready. I don't seem to be able to edit the summary fields or close this bad request.. All I can seem to do is comment. Doing that now then I will try to cleanup.

We have a helper library on top of the driver that wraps calls to UpdateManyAsync with something like the (simplified) code below:

public async Task UpdateBooleanValueToTrue(Expression<Func<MyEntry, bool>> filter)
{
   // Define changes to apply to the database.
   var changes = new List<Tuple<Expression<Func<MyEntry, object>>, object>>();
   changes.Add(new Tuple<Expression<Func<AddressGroupEntry, object>>, object>(entry => entry.Deleted, true));
 
   var mongoUpdateDefinition = ToMongoUpdateDefinition(changes);
 
   var collection = Context.Database.GetCollection<MyEntry>(CollectionName);
   await Collection.UpdateManyAsync(filter, mongoUpdateDefinition);
   // Result handling omitted
}
 
public static UpdateDefinition<TEntry> ToMongoUpdateDefinition<TEntry>(ist<Tuple<Expression<Func<MyEntry, object>>, object>> changes)
{
  var updates = new List<UpdateDefinition<TEntry>>();
  foreach (var change in changes)
  {
    updates.Add(Builders<TEntry>.Update.Set(change.Item1 as dynamic, change.Item2 as dynamic));
  }
  return Builders<TEntry>.Update.Combine(updates);
}

This code works with version 2.18 of the driver and fails with version 2.19. Downgrading to LINQ2 support in version 2.19 allows things to work again.

The failure gives this callstack:

MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: Convert(entry.Deleted, Object).\n   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.ConvertExpressionToFilterFieldTranslator.Translate(TranslationContext context, UnaryExpression expression)\n   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.ExpressionToFilterFieldTranslator.Translate(TranslationContext context, Expression expression)\n   at MongoDB.Driver.Linq.Linq3Implementation.LinqProviderAdapterV3.TranslateExpressionToField[TDocument,TField](Expression\u00601 expression, IBsonSerializer\u00601 documentSerializer, IBsonSerializerRegistry serializerRegistry, Boolean allowScalarValueForArrayField)\n   at MongoDB.Driver.ExpressionFieldDefinition\u00602.Render(IBsonSerializer\u00601 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider, Boolean allowScalarValueForArrayField)\n   at MongoDB.Driver.ExpressionFieldDefinition\u00602.Render(IBsonSerializer\u00601 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)\n   at MongoDB.Driver.OperatorUpdateDefinition\u00602.Render(IBsonSerializer\u00601 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)\n   at MongoDB.Driver.CombinedUpdateDefinition\u00601.Render(IBsonSerializer\u00601 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)\n   at MongoDB.Driver.MongoCollectionImpl\u00601.ConvertWriteModelToWriteRequest(WriteModel\u00601 model, Int32 index)\n   at System.Linq.Enumerable.SelectIterator[TSource,TResult](IEnumerable\u00601 source, Func\u00603 selector)\u002BMoveNext()\n   at System.Collections.Generic.List\u00601..ctor(IEnumerable\u00601 collection)\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable\u00601 source)\n   at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation..ctor(CollectionNamespace collectionNamespace, IEnumerable\u00601 requests, MessageEncoderSettings messageEncoderSettings)\n   at MongoDB.Driver.MongoCollectionImpl\u00601.CreateBulkWriteOperation(IClientSessionHandle session, IEnumerable\u00601 requests, BulkWriteOptions options)\n   at MongoDB.Driver.MongoCollectionImpl\u00601.BulkWriteAsync(IClientSessionHandle session, IEnumerable\u00601 requests, BulkWriteOptions options, CancellationToken cancellationToken)\n  at MongoDB.Driver.MongoCollectionImpl\u00601.UsingImplicitSessionAsync[TResult](Func\u00602 funcAsync, CancellationToken cancellationToken)\n   at MongoDB.Driver.MongoCollectionBase\u00601.UpdateManyAsync(FilterDefinition\u00601 filter, UpdateDefinition\u00601 update, UpdateOptions options, Func\u00603 bulkWriteAsync)\n   at NationalInstruments.NotificationService.DataStore.Providers.Mongo.MongoDataProvider\u00601.UpdateAsync(Expression\u00601 match, Changes\u00601 changes) in /app/NotificationService/NotificationService.DataStore/Providers/Mongo/MongoDataProvider.cs:line 290\n    

 

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