[CSHARP-1734] The FilterDefinition renders errorneously when Any is used inside another Any operator. Created: 05/Aug/16  Updated: 23/Sep/16  Resolved: 09/Aug/16

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.2.4
Fix Version/s: 2.3

Type: Bug Priority: Critical - P2
Reporter: Ioan Crisan Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10 x64



 Description   

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.



 Comments   
Comment by Githook User [ 09/Aug/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1734: fixing issue related to nested Any clauses with conjunctions.
Branch: v2.4.x
https://github.com/mongodb/mongo-csharp-driver/commit/7f2ce4a85d46fca4f48cce6a2121f74a8b47dfcc

Comment by Githook User [ 09/Aug/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1734: fixing issue related to nested Any clauses with conjunctions.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/7f2ce4a85d46fca4f48cce6a2121f74a8b47dfcc

Comment by Ioan Crisan [ 08/Aug/16 ]

Thanks for the hint. We will try to see what we can do in such cases, but we definitely would prefer a driver solution.

Comment by Craig Wilson [ 05/Aug/16 ]

In the meantime, if this is a production problem, you can always step back and not use LINQ for this particular query, but rather use the Builders API and/or manually build up a BsonDocument.

Comment by Craig Wilson [ 05/Aug/16 ]

Hi Ioan,

I'll take a look and see if I can fix it for our next 2.3 release. Depending on complexity, I may push it until later. However, CSHARP-1445 will provide you a workaround in 2.3 if I can't get this one done.

Craig

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