MongoDB gives the ability to write documents of any structure i.e. any number and types of key/value pairs can be written. Assuming that I use this features that my documents are indeed schema-less then how do I manage reads, basically how does the application code ( I'm using Java ) manage reads from the database.
-
Do you want to know how to connect to mongodb using a java client? getting-started-with-java-driverroby– roby2013-08-06 07:19:30 +00:00Commented Aug 6, 2013 at 7:19
-
definitely not. I am trying to reads documents, that have a different structure and instead of hard coding the different structure dependent reads. I am looking for some better efficient way.hershey92– hershey922013-08-06 08:14:03 +00:00Commented Aug 6, 2013 at 8:14
-
1Not all NoSQL databases are schemaless and/or document-oriented. I edited your question and replaced "NoSQL" with "MongoDB", because that seems to be what you mean.Philipp– Philipp2013-08-06 08:32:43 +00:00Commented Aug 6, 2013 at 8:32
1 Answer
The java driver reads and writes documents as BasicBSONObjects, which implement and are used as Map<String, Object>. Your application code is then responsible for reading this map and casting the values to the appropriate types.
A mapping framework like Morphia or Spring MongoDB can help you to convert BSONObject to your classes and vice versa.
When you want to do this yourself, you could use a Factory method which takes a BasicBSONObject, checks which keys and values it has, uses this information to create an object of the appropriate class and returns it.