[JAVA-2424] $set fails when setting sub-document as a Map Created: 12/Jan/17  Updated: 03/May/17  Resolved: 03/May/17

Status: Closed
Project: Java Driver
Component/s: Codecs
Affects Version/s: 3.1.0
Fix Version/s: 3.5.0

Type: Bug Priority: Major - P3
Reporter: Bryan Hunt Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Zip Archive driver.bug.zip    

 Description   

If you try to use $set to update a sub-document as a Map, you get:

Exception in thread "main" 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)
	at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
	at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
	at com.mongodb.client.model.BuildersHelper.encodeValue(BuildersHelper.java:35)
	at com.mongodb.client.model.Updates$SimpleUpdate.toBsonDocument(Updates.java:442)
	at com.mongodb.MongoCollectionImpl.toBsonDocument(MongoCollectionImpl.java:599)
	at com.mongodb.MongoCollectionImpl.update(MongoCollectionImpl.java:542)
	at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:381)
	at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:376)

The attached project will reproduce the failure.



 Comments   
Comment by Githook User [ 03/May/17 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Added MapCodec

JAVA-2424
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/19aac82e2e059341197ad361217b0471208ce6fe

Comment by Ross Lawley [ 26/Apr/17 ]

PR: https://github.com/rozza/mongo-java-driver/pull/190

Comment by Bryan Hunt [ 12/Jan/17 ]

Yes, using Document works, but the code affected by this problem gets the object from a Jackson mapper and writing code to generically walk an object structure looking for all of the Maps is not really feasible. The workaround in my particular case is to force EmberData to use a PUT instead of a PATCH to update the object which does a whole document replace on the back-end instead of trying to use $set.

Comment by Jeffrey Yemin [ 12/Jan/17 ]

Thanks for the report. As a workaround, you can use an instance of org.bson.Document.

Generated at Thu Feb 08 08:57:11 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.