MongoDB Java driver - Object types - java

I need to use the MongoDB Java drive since I need to use the driver within Matlab.
At the moment I have the followed problem. I get my BSON object from database, now I need to convert the BSON tree into a Matlab structure. My problem is that the BSONObject or BasisBSONObject class does not have a function to retrieve the type of the particluar BSON object (ARRAY, OBJECTID, ...). There is a class named BSON in the java driver that defines the values I need. But I do not know how to find out what type my current BSON object is.
The C++ driver and also the C# driver has a function that returns the type of a particular BSON element, but where is it in the JAVA driver.
Any advices are welcome. I'm not oerfect in JAVA maybe I did not find it for this reason...?

Why not get the object and call getClass() on it? myBSON.get("myKey").getClass() Seems like that is just as easy as calling some myBSON.getTypeOf("myKey") method that does not exist and would also be redundant in the API.

Typically I use BSON<->Java POJO mapping libraries like Morphia or Spring-Data-Mongo. These libraries have converters that can convert to and from mongo objects to type-safe objects.
Additionally, I think the Mongo 3.x driver is suppose to have better support for this.

Related

Best way to convert Cassandra ResultSet to java objects in OSS driver

We are in the process of migrating from Cassandra DSE to OSS driver and the pain point is converting the result set into a list of Java objects. I don't want to use Mapper annotation, like detailed here
So what is the most efficient way to convert ResultSet into list of java objects? Any suggestion highly appreciated
If you don't want to use Mapper then just need to write converting function that will take Row, extract all necessary columns, and return POJO constructed from teh extracted data... I don't see other ways, except writing an adhoc version of object mapper that would use the reflection to match POJO's field names with the column names

Unable to read data from Map of Primitive data type from Cassandra DB using Scala

Need help in reading data from Cassandra DB of type Map(varchar,double) in Scala.
i am using Cassandra core 3.0 and data is stored in DB by third party API so i cannot change type
I was trying to read using row.getMap("column_name", classOf[String], classOf[Double])) but i am facing following error: "IllegalArgumentException: Primitive type 'double' used as type parameter"
Thanks in advance.
I believe that the reason of the issue is that getMap returns java.util.Map and standard Java collections do not support value types (aka primitive types) as generic keys or values. Wrapper objects are used instead such as java.lang.Double (see also autoboxing). So try something like
row.getMap("column_name", classOf[String], classOf[java.lang.Double]))

Java ORM: modify record in generic way

Which Java ORM support not-typesave modification of object?
I want to modify objects / records in a generic way, where the fieldName is a string input parameter, and value is a generic AnyObject parameter. Do you know something like this?
I.e. in Core Data in iOS it can work like this:
I went though ormlite tutorial and I just realized, need to get the appropriate Dao, to insert an item:
This is exactly how ActiveJDBC works. Check out at http://javalite.io.

can't serialize class org.joda.money.BigMoney

I am trying to insert into mongo DB and am getting the following error when I try and insert a joda big money object
"can't serialize class org.joda.money.BigMoney"
however according to the java doc BigMoney does implement serializable ( http://www.joda.org/joda-money/apidocs/org/joda/money/BigMoney.html )
Why would this error occur when serializable is implemented?
The Java driver can only serialise simple primitive types not complex ones - it's not using Java serialisation.
If you want to use Joda money you'll have to do the conversion yourself from the BigMoney object into one (or likely more) primitive values that the driver understands.
This will get a lot easier in the 3.x version of the driver, but for now those are your options.

How to read schema-less documents using Java from MongoDB

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.
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.

Categories