Summary
In our solution we are generating dynamic queries based on generics. The folowing snippet worked with Linq2 provider but since the new update (MongoDB.Driver 2.19.0) its broken in the Linq3 provider. __
It has something to do with object type of the property. String property works while ObjectId property doesn't. Looking at how .NET renders the expression it adds a Convert() method around the ObjectId property while not doing that for a string property.
How to Reproduce
No MongoDb instance needed as this just fails to render the query. Attached basic console app reproducing the scenario.
Failing part:
Expression<Func<ConcreteClass, object>> expression = x => x.InternalId; var fieldDefinition = new ExpressionFieldDefinition<ConcreteClass, object>(expression); var query = collection .Aggregate() .Sort(Builders<ConcreteClass>.Sort.Ascending(fieldDefinition)) .ToString();
Additional Background
StackTrace:
Unhandled exception. MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: Convert(x.InternalId, Object). at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.ConvertExpressionToFilterFieldTranslator.Translate(TranslationContext context, UnaryExpression expression) at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.ExpressionToFilterFieldTranslator.Translate(TranslationContext context, Expression 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, Boolean allowScalarValueForArrayField) at MongoDB.Driver.ExpressionFieldDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider) at MongoDB.Driver.UntypedFieldDefinitionAdapter`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider) at MongoDB.Driver.DirectionalSortDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider) at MongoDB.Driver.SortPipelineStageDefinition`1.Render(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider) at MongoDB.Driver.AppendedStagePipelineDefinition`3.Render(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider) at MongoDB.Driver.PipelineDefinition`2.ToString(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider) at MongoDB.Driver.PipelineDefinition`2.ToString(LinqProvider linqProvider) at MongoDB.Driver.AggregateFluent`2.ToString()
- causes
-
CSHARP-4609 InvalidCastException with LinqProvider V3 when passing string as DateTime
- Closed
- is duplicated by
-
CSHARP-4502 UpdateMany call fails after updating from 2.18 to 2.19
- Closed
-
CSHARP-4509 Support Convert calls to a base type in expression translators
- Closed
-
CSHARP-4546 Expression not supported: Convert Error on linq3
- Closed
-
CSHARP-4510 Several expressions no longer translate as expected, even in v2
- Closed
-
CSHARP-4519 UpdateDefinition can't handle casts when combined (multiple Set)
- Closed