Hello,
I have a problem with the Project step in my Facet stage which maps Entity object into DTO:
var dataFacet = AggregateFacet.Create("data",
PipelineDefinition<BankTransaction, BankTransactionDto>.Create(new IPipelineStageDefinition[]
{
PipelineStageDefinitionBuilder.Skip<BankTransaction>((pageNumber - 1) * pageSize),
PipelineStageDefinitionBuilder.Limit<BankTransaction>(pageSize),
PipelineStageDefinitionBuilder.Project<BankTransaction, BankTransactionDto>(x => new BankTransactionDto
{
Id = x.Id,
TransactionDate = x.TransactionDate,
Customer = x.Customer,
Description = x.Description,
Value = x.Value,
CategoryId = x.CategoryId,
CategoryName = x.Category.Name
}),
}));
When I run it, it throws an error âElement âIdâ does not match any field or property of classâ. Here entire stack:
System.FormatException: Element 'Id' does not match any field or property of class SaveMeter.Modules.Transactions.Core.DTO.BankTransactionDto.
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeClass(BsonDeserializationContext context)
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context)
at MongoDB.Bson.Serialization.Serializers.EnumerableSerializerBase`2.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context)
at MongoDB.Driver.AggregateFacetResultsSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context)
at MongoDB.Bson.Serialization.Serializers.EnumerableSerializerBase`2.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context)
at MongoDB.Driver.Core.Operations.AggregateOperation`1.CursorDeserializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context)
at MongoDB.Driver.Core.Operations.AggregateOperation`1.AggregateResultDeserializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context)
at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.ProcessResponse(ConnectionId connectionId, CommandMessage responseMessage)
at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocolAsync[TResult](IWireProtocol`1 protocol, ICoreSession session, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.RetryableReadOperationExecutor.ExecuteAsync[TResult](IRetryableReadOperation`1 operation, RetryableReadContext context, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.ReadCommandOperation`1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.AggregateOperation`1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.AggregateOperation`1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperationAsync[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.AggregateAsync[TResult](IClientSessionHandle session, PipelineDefinition`2 pipeline, AggregateOptions options, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
at MongoDB.Driver.IAsyncCursorSourceExtensions.FirstAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
at SaveMeter.Modules.Transactions.Core.Queries.Handlers.GetBankTransactionsByFilterHandler.HandleAsync(GetBankTransactionsByFilter query, CancellationToken cancellationToken) in /Users/mawb/learning/repos/savemeter-api-monolith/src/Modules/Transactions/SaveMeter.Modules.Transactions.Core/Queries/Handlers/GetBankTransactionsByFilterQueryHandler.cs:line 70
The interesting fact, when I rename âIdâ property into âTransactionIdâ in DTO object, everything works fine. So I assume, that something is wrong with the property name âIdâ. Also, there is no error when I remove Project step.
Iâm using MongoDB.Driver 2.18.0
Example document:
{
_id: UUID("8edbc21e5a214672b4f04a91bac5e0c9"),
_t: [
'Entity',
'BankTransaction'
],
CreatedAt: ISODate('2022-11-01T07:55:21.840Z'),
UpdatedAt: ISODate('2022-11-01T07:55:21.840Z'),
TransactionDate: ISODate('2022-03-14T00:00:00.000Z'),
Customer: 'Example customer',
Description: 'Example description.',
Value: NumberDecimal('-72.99'),
CategoryId: UUID("792badf0b21642f1a312c105d1b87051"),
BankName: 'null',
UserId: UUID("...")
}
This document is mapped with BsonClassMap:
BsonClassMap.RegisterClassMap<Entity>(map =>
{
map.SetIsRootClass(true);
map.MapIdMember(x => x.Id);
map.MapMember(x => x.CreatedAt);
map.MapMember(x => x.UpdatedAt);
});
BsonClassMap.RegisterClassMap<BankTransaction>(map =>
{
map.AutoMap();
map.SetIgnoreExtraElements(true);
map.MapMember(x => x.Value).SetSerializer(new DecimalSerializer(BsonType.Decimal128));
map.GetMemberMap(x => x.Category).SetShouldSerializeMethod(_ => false);
});
And classes used in example:
internal class BankTransaction : Entity
{
public DateTime TransactionDate { get; set; }
public string Customer { get; set; }
public string Description { get; set; }
public decimal Value { get; set; }
public Guid? CategoryId { get; set; }
public Category Category { get; set; }
public string BankName { get; set; }
public Guid UserId { get; set; }
}
public record BankTransactionDto
{
public Guid Id { get; init; }
public DateTime TransactionDate { get; init; }
public string Customer { get; init; }
public string Description { get; init; }
public decimal Value { get; init; }
public Guid? CategoryId { get; init; }
public string CategoryName { get; init; }
}
I forgot to mention that Iâm using lookup operation to join Category, but I tested it without lookup and had the same error.