For my project I use Morphia, for easily mapping POJO objects to the Mongodb database. But in 2018 the mongo java driver started supporting pojo mapping it self and the Morphia project was abandoned by the Mongodb team. The Morphia community edition has now deprecated the DAO and I wonder, why not write my own DAO class based on the Mongodb driver directly? So my question:
Do we still need Morphia when using Mongodb and Java? And what benefits does Morphia bring over using the Mongodb Java driver directly?
Example code for using the Java driver:
// Getting an object by id:
collection.find(eq("_id", id), Pojo.class).first();
// Insert into collection
collection.insertOne(pojo);
// Update
collection.replaceOne(eq("_id", id), pojo);
// Delete
collection.deleteMany(filter).getDeletedCount();
I'm the morphia dev so there's some bias here but i'll try to be fair. when i first started building the pojo support in to the driver (i used to work for mongodb) my goal was to build as close to an ODM as possible in the driver so that morphia, such as it was, would only need to be a thin veneer on the driver. Some of my plans never came to fruition as I left the company mid-effort. That said, it came pretty close.
I know of several projects that are happily using the pojo codecs. If they fit your needs, then I would suggest to just go with that. For my own perspective, I think morphia offers a fair bit that the driver doesn't (to my knowledge.) Morphia supports annotation driven index and document validation definitions, and collection caps, e.g. It's a bit more powerful and forgiving in mapping. e.g., morphia can map non-String keyed Maps as fields and I don't think the driver supports. Morphia supports lifecycle events but the driver does not and last I checked it seemed like Morphia's generics support had a slight edge. (Granted that edge is probably an extreme edge case that most won't run in to. It's been a while so the details are fuzzy.)
There are a few other features that Morphia has that the driver doesn't (transparent reference support, e.g.) and some features I have planned that the driver will never support (build-time generated codecs to eliminate most/all reflection at runtime, e.g.).
So do we still need Morphia? It depends on what you want to do. I plan on working on Morphia until no one needs it, though. :)