The following code sequence is used to generate the error:
var documentSerializer = BsonSerializer.SerializerRegistry.GetSerializer<T>();
var renderedFilter = filterDefinition.Render(documentSerializer, BsonSerializer.SerializerRegistry).ToString();
The FilterDefinition contains the expression obtained from:
tours.Where(tour => tour.CarrierAssociations.Any(x => x.CarrierId == myOrganizationId && x.States.Any(y => y.State == TourState.AcceptPending))).ToList();
The output is:
{ "carrierAssociations" : { "$elemMatch" :
{ "carrierId" : NumberLong(22), "state" : 0 }} }
which is not correct, because "state" is an array of subdocuments, which in turn contain a "state" field. I assume the correct output would have been:
{ "carrierAssociations" : { "$elemMatch" :
{ "carrierId" : NumberLong(22), "state.state" : 0 }} }
or something like that. Note: the "state" names that appear in the JSON are field names specified with the BsonElement attribute, so please do not be confused about them. I don't know exactly which "state" does the output refer to: the "States" collection property, or the "State" within the the elements of this collection - by chance they have the same BSON element name.
I marked it as critical because we have this problem in production, and we do not have at least a workaround for this. Moreover, we identified this in one place, but we are not sure if the application code contains other similar queries, which we did not identified yet, and which could cause hidden bugs.