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

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.

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 to Mike Hostetler Cancel reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Rockstar Development
This originally appeared on Marty Henderson’s personal blog Or, how to use Gitpod and GitLab so that no one else has to care about your questionable coding language choices. A true rockstar has a good […]
Testing a Quarkus Kafka Application
Quarkus, a “Kubernetes Native Java stack,” enables lighter Java applications with faster startup times. In a recent post, I talked about scaling Kafka consumers in Kubernetes. Quarkus applications fit right into this picture because they […]
Gitpod and Hringvegurinn
Iceland Ever seen an advertisement for visiting Iceland? Have you noticed that they all mention Hringvegurinn or the Ring Road, as a good tour? (If you haven’t seen a tour ad for Iceland, Steindi Jr […]
Tacos and Steak, an Istio story
This post originally appear on Marty Henderson’s personal blog A brief history In the before times of 2017, Varun Talwar and Louis Ryan sat under a forgotten waterfall at the edge of the world and […]