Official Mongo Java Driver Vs Morphia

1,924 views
Skip to first unread message

Karthik

unread,
Feb 3, 2011, 12:58:48 PM2/3/11
to Morphia
Hi,

I have tried morphia and quite liked it to use for MongoDB
implementations. I have tried the official mongo DB Java driver as
well (Map implementation of BasicDBObject), that is also quite
comfortable to work with.

I am looking into what exact advantages morphia has over MongoDB
java driver. Could any one list down the advantages of morphia over
mongo java driver.

Stephen Gregory

unread,
Feb 3, 2011, 1:18:51 PM2/3/11
to mor...@googlegroups.com
They're not exactly mutually exclusive.  In fact, I use both. 

Morphia allows you to persist and query POJOs from your mongo collections.

The mongo driver operates on map-like data model.  If you're trying to persist your own POJOs to and from a mongo db, you'd have to write a lot of code to generate your POJO from a map and vice-versa.  If you're just using maps anyway, then Morphia probably doesn't do a lot for you. Morphia also adds some nice syntactic sugar on top of the vanilla mongo driver that allows you to just work in terms of your POJOs instead of DBObjects and maps.  This can help you keep a lot of the persistence specific code out of your Domain objects.  There are annotations sure, but that's another debate.

Michael Gray

unread,
Feb 4, 2011, 9:44:14 AM2/4/11
to mor...@googlegroups.com
Stephen is pretty much right..

MongoDB (and it's driver) is really a DB interface.  Technically Mongo is more of a "document" management system than a database management system, but that's a different conversation.

Morphia is really a  Persistence layer, for persisting Java objects into Mongo.

It really depends on what you are trying to do.  If you want to store "Objects" in your MongoDB, than Morphia will make your life easy, since you can just use annotations as you define your Java objects.   Want to add a new field to your object? Just add the field to your class definition and annotate it appropriately, and Morphia does all the work mapping the object in and out of Mongo.

However, sometimes you may really just want direct DB access.  For example, maybe you want to generate a JSON like document that aggregates 100 different records, and then returns them in some giant JSON document (like a RSS stream, or something similar), which you are just going to return to the user (maybe as part of a REST-like interface).    You don't need all the overhead of using a persistance layer (which could slow you down, and use more memory than needed).

A good rule of thumb...

If your operation requires,  Get an OBJECT, and have the object DO SOME OPERATION (ex:execute a method on the object), than you probably want a PERSISTANCE layer (Morphia, Hibernate, etc).

If you want to just lookup and a JSON/BSON record and then RETURN a JSON record (maybe a REST lookup), you may just want to use the Driver directly, and avoid the overhead of allocating and freeing all those POJOS.    This is especially true if you are returning a large result that iterates over a whole collection and returns lots of records in a single query.

Example:  When a user executes a new transaction, you might use a Persistence layer, since that transaction might have lots of steps before it's done, and it has lots of Java methods, etc.  

Later you want to return a list of every transaction a user has ever made in your system.  It would be more efficient to use the Driver directly, both in terms of speed and memory (no need to instantiate a POJO for each transaction the guy ever did).

A rule of thumb...
If you need to Read a record, do something mildly complex, and then write the result, think persistance.
if you just need to Read and Write (simple GET or WRITE), than think DB layer.   
--
Michael Gray
Founder
Technology Evangelist
MyPlanit.com


Karthik

unread,
Feb 4, 2011, 10:14:56 AM2/4/11
to Morphia
Awesome Michael . That was a great explanation. That gives me more
insight on using morphia vs mongo driver based on the scenario.

Thank you
> On Thu, Feb 3, 2011 at 1:18 PM, Stephen Gregory <sgreg...@bostechcorp.com>wrote:
>
>
>
>
>
>
>
>
>
> > They're not exactly mutually exclusive.  In fact, I use both.
>
> > Morphia allows you to persist and query POJOs from your mongo collections.
>
> > The mongo driver operates on map-like data model.  If you're trying to
> > persist your own POJOs to and from a mongo db, you'd have to write a lot of
> > code to generate your POJO from a map and vice-versa.  If you're just using
> > maps anyway, then Morphia probably doesn't do a lot for you. Morphia also
> > adds some nice syntactic sugar on top of the vanilla mongo driver that
> > allows you to just work in terms of your POJOs instead of DBObjects and
> > maps.  This can help you keep a lot of the persistence specific code out of
> > your Domain objects.  There are annotations sure, but that's another debate.
>

Scott Hernandez

unread,
Feb 4, 2011, 10:29:44 AM2/4/11
to mor...@googlegroups.com

While I mostly agree with this some people might draw the line closer
to whether you need dynamic properties or if you can model your data.

There is some overhead to converting the data to a pojo, but you also
get much simplier access and don't need to deal with type conversion
on your own, or checking for nulls (missing values).

In the end, you are welcome to use the driver (directly) as much as
you like and Morphia can co-exist very well where some things are done
with the driver and some with Morphia. There are even helper methods
on the datastore to get the DB/Collection object from your
queries/operations.

Reply all
Reply to author
Forward
0 new messages