[CSHARP-3691] GuidRepresentationMode.V3 filter in project of aggregation throws InvalidOperationException Created: 27/May/21 Updated: 28/Oct/23 Resolved: 15/Oct/21 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | LINQ3 |
| Affects Version/s: | 2.12.3 |
| Fix Version/s: | 2.14.0 |
| Type: | Bug | Priority: | Unknown |
| Reporter: | Arne Schoonvliet | Assignee: | Unassigned |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Description |
|
When using GuidRepresentationMode.V3 you cannot filter on GUID in a project using the aggregation framework. The code throws it's expecting GuidRepresentationMode.V2.
Below you can find a simple example:
Below you can find the exception
Same exception is happing with version 2.13.0-beta of the driver |
| Comments |
| Comment by Ahiya Elster [ 06/Jul/23 ] | ||||||||||||
|
This fixed it, thanks for the quick response! | ||||||||||||
| Comment by James Kovacs [ 04/Jul/23 ] | ||||||||||||
|
The root cause of the problem is the use of the implicit conversion from Guid to BsonValue. (See BsonValue.cs line 731.) This implicit conversion is only valid for GuidRepresentationMode.V2. In V3 mode, you must explicitly specify the representation of each Guid as different Guid values within a document can have different representations. Because you are specify the pipeline stage as a BsonDocument, there is no BsonClassMap or BSON attributes to look up to determine how it should be mapped. In order to specify the GuidRepresentation, you cannot use the implicit conversion, but must instead use the BsonBinaryData constructor as shown below.
Hope this answers your question. Sincerely, | ||||||||||||
| Comment by Ahiya Elster [ 04/Jul/23 ] | ||||||||||||
|
Hi, I'm getting the error when I run the following code:
And the exception is: System.InvalidOperationException: This constructor can only be used when BsonDefaults.GuidRepresentationMode is V2.
I also have the following code in my startup class:
If possible I wish to use the built in aggregation pipeline instead of the LINQ alternative Is there any possible fix that I can do for this? Mongo c# driver version: 2.19.1 Mongo db version: 5.0.14
Thanks | ||||||||||||
| Comment by Arne Schoonvliet [ 17/Oct/21 ] | ||||||||||||
|
Hi Dmitry Thanks for the update! I will check it out! | ||||||||||||
| Comment by Dmitry Lukyanov (Inactive) [ 15/Oct/21 ] | ||||||||||||
|
This issue has been fixed in the new LINQ provider (known as LINQ3) which will be included in the upcoming 2.14 release. Configure your MongoClientSettings to use LinqProvider.V3 if you want to use this functionality. To configure a client to use the LINQ3 provider use code like the following
| ||||||||||||
| Comment by James Kovacs [ 03/Jun/21 ] | ||||||||||||
|
Hi, Arne, We are currently completing a code review of the new LINQ implementation before merging it into the main repository. Our goal is to ship it as an opt-in replacement for our current LINQ provider in an upcoming beta release of 2.13.0. Sincerely, | ||||||||||||
| Comment by Arne Schoonvliet [ 03/Jun/21 ] | ||||||||||||
|
Hi James Thanks for checking and confirming the problem. What is the ETA for the new driver with it's new LINQ implementation? I guess this will be the 3.x.x version together with mongodb 5.0.0? Is it possible to checkout a certain branch of the driver on github so I can already check some other parts of our code with the new driver already? Sincerely | ||||||||||||
| Comment by James Kovacs [ 02/Jun/21 ] | ||||||||||||
|
Hi, Arne, Thank you for reporting this issue. We have reproduced the problem and identified the cause. When generating an array filter expression containing a GUID during the project stage, we assume V2 GuidRepresentationMode to translate the GUID, which is invalid when in V3 GuidRepresentationMode. If you remove the following portion of the project, the query is generated and executed successfully:
Returning the entire array is also successful:
As is filtering on a non-GUID field. (Name field added to the inner class.)
We have confirmed that this issue is resolved in our new LINQ implementation, which will ship in an upcoming version of the driver. Thank you again for reporting this issue. You can monitor this issue for updates on the availability of a fix. Sincerely, | ||||||||||||
| Comment by Mikalai Mazurenka (Inactive) [ 28/May/21 ] | ||||||||||||
|
Thanks for reporting this issue! We need some time to investigate it and will come back to you. | ||||||||||||
| Comment by Arne Schoonvliet [ 27/May/21 ] | ||||||||||||
|
One thing to note, code above works when using GuidRepresentationMode.V2 and GuidRepresentation.Standard as global default. |