Summary
MongoDB.Driver since version 1.19.0 does not support the extension method inside the Project() method. In version 2.19.1 this still doesn't work.
How to Reproduce
After updating MongoDB.Driver from 2.18.0 to 2.19.1, I got an exception:
MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: execution.ToExecutionCommonDto().
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodCallExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, MethodCallExpression expression)
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, Expression expression)
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(TranslationContext context, LambdaExpression lambdaExpression, IBsonSerializer parameterSerializer, Boolean asRoot)
at MongoDB.Driver.Linq.Linq3Implementation.LinqProviderAdapterV3.TranslateExpressionToProjection[TInput,TOutput](Expression`1 expression, IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)
at MongoDB.Driver.Linq.Linq3Implementation.LinqProviderAdapterV3.TranslateExpressionToFindProjection[TSource,TProjection](Expression`1 expression, IBsonSerializer`1 sourceSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.FindExpressionProjectionDefinition`2.Render(IBsonSerializer`1 sourceSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOperation[TProjection](FilterDefinition`1 filter, FindOptions`2 options)
at MongoDB.Driver.MongoCollectionImpl`1.FindAsync[TProjection](IClientSessionHandle session, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at MongoDB.Driver.FindFluent`2.ToCursorAsync(CancellationToken cancellationToken)
at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
Where the query code looks like this:
return await this.databaseContext.Executions .Find(this.databaseContext.CurrentSession, filterBuilder.And(filters)) .Project(execution => execution.ToExecutionCommonDto()) .ToListAsync(CancellationToken.None);
I use the public extension method ToExecutionCommonDto() that converts the database model to the DTO model:
return new ExecutionCommonDto { Id = execution.Id, StartTime = execution.Time?.StartTime, EndTime = execution.Time?.EndTime, Timeout = execution.Time?.Timeout, Status = execution.Status?.Status, Result = execution.ExecutionResult?.Result };
And this code worked in version 1.18.0, but since version 1.19.0 it stopped working and in 1.19.1 they still don't work.
I'm using MongoDB inside container:
mongoDb: image: bitnami/mongodb:5.0.3 restart: always ports: - "27017:27017" environment: - MONGODB_DATABASE=database - MONGODB_USERNAME=user - MONGODB_PASSWORD=password - MONGODB_ADVERTISED_HOSTNAME=mongoDb - MONGODB_REPLICA_SET_MODE=primary - MONGODB_ROOT_PASSWORD=password - MONGODB_REPLICA_SET_KEY=C1D6B99752EC4929 - MONGODB_SYSTEM_LOG_VERBOSITY=0 volumes: - mongoDb_volume:/bitnami:rw networks: - app-network mongoDb-replica: image: bitnami/mongodb:5.0.3 restart: always privileged: true environment: - MONGODB_ADVERTISED_HOSTNAME=mongoDb-replica - MONGODB_REPLICA_SET_MODE=secondary - MONGODB_INITIAL_PRIMARY_HOST=mongoDb - MONGODB_INITIAL_PRIMARY_PORT_NUMBER=27017 - MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=password - MONGODB_REPLICA_SET_KEY=C1D6B99752EC4929 - MONGODB_SYSTEM_LOG_VERBOSITY=0 depends_on: - mongoDb networks: - app-network mongoDb-arbiter: image: bitnami/mongodb:5.0.3 restart: always privileged: true environment: - MONGODB_ADVERTISED_HOSTNAME=mongoDb-arbiter - MONGODB_REPLICA_SET_MODE=arbiter - MONGODB_INITIAL_PRIMARY_HOST=mongoDb - MONGODB_INITIAL_PRIMARY_PORT_NUMBER=27017 - MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=password - MONGODB_REPLICA_SET_KEY=C1D6B99752EC4929 - MONGODB_SYSTEM_LOG_VERBOSITY=0 depends_on: - mongoDb networks: - app-network
- duplicates
-
CSHARP-4498 .Project(c => c.Reference()) not work
- Closed
-
CSHARP-4763 Consider supporting client side projections with Find and Select in LINQ3
- Closed