Nov 28, 2017

Missing Codecs in MongoDB

When upgrading a project’s Mongo driver from 2.13.0 to 3.4.2, we got the following exception:

org.bson.codecs.configuration.CodecConfigurationException: can't find a codec for class java.math.BigDecimal.

Well it seems that Mongo doesn’t support BigDecimal any longer. There we made our own BigdDecimal Codec. If you search for the “can't find a codec for class” error, you inevitably end up at this StackOverflow entry. However that is about saving nested, custom objects into Mongo. If you search for a while longer you will end up in the Mongo docs that has a simple example with Integer. Seems easy to adopt that for BigDecimal.

But when we did that, we got the same error. it seemed like Mongo was just ignoring our call to the CodecRegistry! The problem was what is described here – we were using a BasicDBObject and not a Document so the Mongo driver wasn’t hitting the CodecRegistry.

I made a Gist overview of how we got it working. The key was adding the Transformer via the static class with this line:  BSON.addEncodingHook(BigDecimal.class, new BigDecimalTransformer()); . We needed both that Transformer and the Codec in the registry for it to work.

I hope that this post can save someone else a few hours/days of head-scratching.

About the Author

Object Partners profile.

One thought on “Missing Codecs in MongoDB

  1. Joerg says:

    Thanks a lot for this post! Had a similar problem when upgraded Apache Camel to a new version (which also includes upgrade of MongoDB Driver), which broke the software I was working on. Camel community pointed out that this is “wanted-by-design” by the new mongo driver, but that didn’t help getting my code work again.
    Than I Implemented a BigDecimal codec like you suggested in your GIST example, worked like a charm!

    So, indeed, your post saved me a LOT “hours/days of head-scratching” :)!

    1. I’m glad my post helped! The fix is not intuitive.

  2. MarcE says:

    Many thanks! Really good work!

Leave a Reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]