Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Type Mapping In MongoDB

Let me start by saying my goal is to achieve the fastest read operations I can, so if you spot something in my code/description that could help me achieve that, please let me know.

I have a read only web application that is pulling objects from MongoDB. However, the response object which I will be returning to the user will not need all of the data. As it stands right now, the process is as follows:

  • Retrieve information from mongo
  • Map that information to an object
  • Use accessor methods to transfer values from said object to the response object.
  • Return response object to user.

I would like to combine steps 1 and 2 by just having the data coming out of mongo mapped directly to the response object.

I have dug through the DefaultMongoTypeMapper and all of the classes it utilizes, but it is a lot to wrap my head around before I am certain it is the right direction to go.

Besides what I have listed above, I have a few other concerns. One being that the objects stored in Mongo do not have the _class field, because they are raw data (from what I have seen, the type mapper uses that field to determine the class to map to). The upside to this is that everything coming out of the database will be mapped to the same object, so we can assume what class to use.

Has anybody done something like this is the past, or have any suggestions as to how this can be accomplished?

Much appreciated.

like image 612
Mac Avatar asked Oct 02 '22 16:10

Mac


1 Answers

If you're looking for fastest read operations, probably the first place to look is the speed of the actual read, and transfer across the network, with MongoDB. The processing within Java (within reason) will be a very low % of the total request time. You may have already done this, but take a look at the raw reads from MongoDB (indexes, query, number of records, database schema, configuration etc)

For the java processing it is very dependant on what you want to do with the results. I'd suggest you create some tests for the various options for java processing, this is likely CPU bound. If you can create a test that processes many records at a time, you can see the performance characteristics of each.

Could you manually write the code the create POJOs from the document? this may well be the most performant. Otherwise an ODM like morphia https://code.google.com/p/morphia/

If you are familiar/using Spring (as mentioned above) already See: http://projects.spring.io/spring-data-mongodb/ maybe take a look at: http://spring.io/guides/gs/accessing-data-mongo/

like image 119
Alan Spencer Avatar answered Oct 07 '22 21:10

Alan Spencer