简体   繁体   中英

MongoDB Java driver - Object types

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM