04 Aug 2017, 10:10

MongoDB POJO Support

I’m really pleased to announce that version 3.5.0 of the MongoDB Java Driver has been released with POJO (Plain Old Java Object) support!

Codecs

MongoDB uses BSON, a binary super set of JSON, for its wire protocol and storage format. The 3.0 series of the Mongo Java Driver introduced Codecs - an improved way of translating these BSON into the native Java objects eg: Document or BsonDocument.

Codecs are an abstraction that determine how BSON data is converted and into what type. As an abstraction, it can be quite verbose to write your own custom POJO Codecs. As each POJO requires a Codec implementation to be registered in the CodecRegistry. The amount of code required to support an application with tens of POJOs was often seen as a barrier to entry.

However, the benefits of using Codecs for handling your POJOs were numerous. It could easily simplify your main application code, as POJOs can map directly to the domain, making the code easier to reason. Another benefit is speed, Codecs can negate the need to use an intermediate map-like object before hyrdating your domain object. For this reason, it has been a long requested feature to make the creation of Codecs from POJOs automatic.

PojoCodecProvider