[CSHARP-2149] A class inside an embedded array is not being serialized when Projection.ElemMatch is used Created: 14/Jan/18 Updated: 27/Oct/23 Resolved: 11/Feb/19 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Read Operations |
| Affects Version/s: | 2.4.4, 2.5 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Critical - P2 |
| Reporter: | Itzhak Kagan | Assignee: | Wan Bachtiar |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
windows 7sp1, visual studio 2017, driver 2.4.4, 2.5.0, server 3.4.10 |
||
| Attachments: |
|
| Description |
|
A class inside an embedded array is not being serialized when Projection.ElemMatch is used I have classes like this:
I constructed a query:
with the following result:
However the c# object does not serialize the 'Member' property and it remains null Thanks, |
| Comments |
| Comment by Wan Bachtiar [ 17/Jun/19 ] | |||||||||||||||||||||||||||||||||||||||||||||
Hi Itzhak, The syntax for ProjectionDefinition accepts two parameters, TSource as source map and TProjection as the projection map. In your example you specify the projection map type to be AccountMemberDal. However the returning document from the server is still in the shape of AccountDal, having a field Members. Based on your example class mappings, we can use example documents as below in accounts collection:
If you were to run a similar query in mongo shell, it would look something as below:
The resulting documents would look as below:
As you can see the results contain Members but the mapping AccountDalMembers does not have Members property to deserialise the results.
Your example query is looking for IsAdmin=true but the documents shown in Watch have false. In addition, the value Permissions is also None. Perhaps they're just showing default values due to failed deserialisation ?
Changing the TProjection type of the ProjectionDefinition doesn't change the amount of data traffic from the database. This is just changing the deserialisation class map from AccountMemberDal to AccountDal. Regards, | |||||||||||||||||||||||||||||||||||||||||||||
| Comment by Itzhak Kagan [ 12/Feb/19 ] | |||||||||||||||||||||||||||||||||||||||||||||
|
Hi Wan, Thanks for your answer. I know that it is not because of you, that this issue was addressed more than a year after it was opened. Pay attention that your suggestion changes completely the projection of the query. Your suggestion is to return the entire AccountDal objects which will increase the network traffic. I did not understand this sentence: however AccountMemberDal doesn't have an attribute to deserialise this field into List<AccountMemberDal> Members. The {{AccountMemberDal is a class that is used in the }}AccountDal class to define a list public List<AccountMemberDal> Members { get; set; } As you can see from the attachment picture: The result is indeed a list of AccountMemberDal, the only problem is that the Member property is null and is not being populated. | |||||||||||||||||||||||||||||||||||||||||||||
| Comment by Wan Bachtiar [ 04/Feb/19 ] | |||||||||||||||||||||||||||||||||||||||||||||
Hi Itzhak, I think the problem here is due to the projection from AccountDal to AccountMemberDal. As can be seen from your result example, it contains field called members, however AccountMemberDal doesn't have an attribute to deserialise this field into List<AccountMemberDal> Members. You could try replacing the projection code to:
Please note that the CSHARP project is for reporting bugs or feature suggestions for the MongoDB .NET/C# driver. If you have any follow-up questions on the use of the driver, please post a question on mongodb-user group with relevant the information. Regards, |