Hi,
I'm using Java driver 3.1.0. I have a document with a field ("myMapField") that is populated with a Map<String, Object>. I am able to insert the document (as a Document) but I can't update the field. Here's how I'm trying to update the field:
myCollection.updateOne(filter, set("myMapField", myMap));
I get the following exception:
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.util.HashMap.
at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46) ~[bson-3.1.0.jar:na]
Looking at the source code I see why I'm able to insert but not update: org.bson.codecs.DocumentCodec encodes a Map by iterating over the EntrySet, writing each value individually. com.mongodb.client.model.BuildersHelper takes a different approach and looks in the registry for the class, hence the exception. The same thing happens with enums - I can insert an enum but can't update a field with an enum.
Is there some reason updates are different than inserts? BuildersHelper is final so it can't be extended, though there's probably no way to insert a custom BuildersHelper anyway. It would really be helpful if BuildersHelper took the same approach as DocumentCodec when encoding values.
Paul