[CSHARP-4707] System.FormatException when trying to rename a field in the projection step of an aggregation pipeline Created: 04/Jul/23 Updated: 27/Oct/23 Resolved: 19/Jul/23 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | LINQ3 |
| Affects Version/s: | 2.18.0 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Unknown |
| Reporter: | David Wolsey | Assignee: | Robert Stam |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Documentation Changes Summary: | 1. What would you like to communicate to the user about this feature? |
| Description |
|
I'm trying to using an aggregation projection stage to convert input documents to output documents with calculated properties. Using a C# expression with a definite type, e.g.
results in
whereas defining the projection step using a BsonDocument as below works as expected
Is there some way to build valid typed projections via C# expression tree when the output property names don't match the input property names? I thought this may have been fixed by the V3 Linq provider but that seems to give the same exception. |
| Comments |
| Comment by Robert Stam [ 19/Jul/23 ] | |||||||||||||||||||||||||||||||||||||||||||||
|
That way of configuring the LINQ provider looks correct. If you are using version 2.19.0 or above LINQ3 is already the default and you wouldn't have to do that. Since you can confirm that it is working with 2.19.0 onwards I will close this ticket. Thanks for the info. | |||||||||||||||||||||||||||||||||||||||||||||
| Comment by David Wolsey [ 11/Jul/23 ] | |||||||||||||||||||||||||||||||||||||||||||||
|
Interesting, thanks Robert. In the repro linked above I am configuring the LINQ provider like this
Is that not the correct way to configure the v3 provider? I can confirm however that 2.19.0 onwards works as expected. | |||||||||||||||||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 10/Jul/23 ] | |||||||||||||||||||||||||||||||||||||||||||||
|
With the additional information you provided I was in fact able to reproduce the issue you saw. However, I was also able to confirm that this is only an issue when using LINQ2. It works fine in LINQ3. You mentioned that you were using version 2.18.0 of the driver. In that version LINQ2 was the default. Starting with version 2.19.0 LINQ3 became the default, but you can also configure that you want to use LINQ3 in version 2.18.0 in the MongoClientSettings. Can you upgrade to the current version of the driver? | |||||||||||||||||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 10/Jul/23 ] | |||||||||||||||||||||||||||||||||||||||||||||
|
Thanks. That's helpful. I had not been able to reproduce this but I wasn't using Facet. | |||||||||||||||||||||||||||||||||||||||||||||
| Comment by David Wolsey [ 10/Jul/23 ] | |||||||||||||||||||||||||||||||||||||||||||||
|
I've had a bit more of a play with this and created a reproduction at https://github.com/webley/mongodb-projection
It seems that projections built like above work as expected in a simple aggregation pipeline but fail when they are within a $facet stage. For instance, the following code results in the FormatException described above.
Whereas passing the PipelineDefinition defined above into _taskCollection.Aggregate().AppendStage() works as expected, as does passing the ProjectionDefinition into _taskCollection.Aggregate().Project(). If it helps, the pipeline held in the `facet` variable above is defined as:
(Obviously, I'm just showing a pipeline with a single facet for the sake of brevity, but we would normally have multiple!) Thanks for any help you can offer. | |||||||||||||||||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 06/Jul/23 ] | |||||||||||||||||||||||||||||||||||||||||||||
|
Can you also let us know how you are using the projection? I assume you are passing it to the `IAggregateFluent.Project` method? Ideally you could provide us with a complete standalone reproduction, but the more information you give us the more likely we can reproduce what you are seeing. | |||||||||||||||||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 06/Jul/23 ] | |||||||||||||||||||||||||||||||||||||||||||||
|
Can you please provide the C# class declarations for the TaskEntity and TaskHeader classes? That will help us reproduce the issue. | |||||||||||||||||||||||||||||||||||||||||||||
| Comment by PM Bot [ 04/Jul/23 ] | |||||||||||||||||||||||||||||||||||||||||||||
|
Hi david.wolsey@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon. |