We have a very dynamic data model and persist our data untyped (the model’s value property is of type object). Inserts work well, since then our custom serializer is at work which knows which type it is handling and can serialize lists very well.
Now we previously had mixed Ids. ObjectId for document Ids and usually GUID as Ids for finding the correct element in nested arrays. For references in those dynamic data fields we use List. This worked well so far.
Now we need to refactor this into using ObjectIds everywhere since we want to build a system which needs all those Ids and having to check and store whether it’s Guid or ObjectId is tedious and imperformant.
But now when we want to serialize a List into a field that’s just “object”, it fails. First due to it being an not allowed type for the
ObjectSerializer. But configuring it to accept a List works around that. Now when we update a field, the value remains null, altough MongoDB says everything worked fine.
Why is this so? Can someone explain me how I can serialize this list?
Thanks for reaching out to us about this issue. It sounds like you’ve configured
ObjectSerializer.AllowedTypes correctly if you are able to serialize the IDs as a
List<object>. It’s not clear why the value would remain null. I would encourage you to file a CSHARP issue with the following so we can investigate further:
- a small, self-contained repro
- driver version
- server version
Hi James, thanks for the quick response.
I forked the driver and made a test project reproducing the issue. In this isolated environment I could actually find the source (our data model is huge and there’s a few steps to produce the situation, making it hard to isolate).
I found the issue was not the serializer but the array filter we’re using. Since those have no support for Expressions we had to hard-code them. Previously we used Guids for nested entities’ Ids, so the field was named differently to separate it from regular “Id” (or in DB: _id) fields.
Due to the refactoring we also changed them to use ObjectIds and thus MongoDB automatically applied the ‘_id’ naming even though these were nested documents and no unique fields. This broke the array filter.
Sadly MongoDB does not communicate issues with array filters very well in some scenarios. That’s maybe something that could be improved. Obviously “Id” or “id” is a valid name but it does not match if the field’s actually called “_id” inside MongoDB.
Long story short: Not a bug, just invalid usage with too few feedback to identify the issue correctly.
Glad that you were able to find the root cause of the problem. Interesting about the unclear errors/feedback related to array filters. This sounds more like a server query issue than a .NET/C# Driver issue. If it is in fact the former, you can file a https://jira.mongodb.org/browse/SERVER issue. If it does turn out to be the latter (that the driver is not communicating array filter problems in a meaningful way), my team would be happy to investigate further if you file a https://jira.mongodb.org/browse/CSHARP issue.
Thanks for closing the loop on the investigation and confirming that this isn’t inherently a driver bug, but more a result of unclear error messages.