Java: how to cast Bson object of unknow type ...

68 views
Skip to first unread message

Oscar themacboy

unread,
Jul 13, 2016, 4:40:06 AM7/13/16
to BSON
Good morning,

Im working with a external database that some in one of his fields the bson document returned can change from bsnon object to and Array of bson.

Is htere a way in java to know the type returned before cast?

Thx in advance.

Oscar themacboy

unread,
Jul 13, 2016, 6:07:31 AM7/13/16
to BSON
Finally found how to solve:

· Create A BsonDocument object.
· Get the BsonValue to evaluate.
· check if is an Array.

Example:

        BsonDocument myBson= myDocuement.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry());
        BsonValue myBsonValue= myBson.get("skill");

        if (myBsonValue.isArray()) {
            System.out.println("Hello!");
        } else {
            System.out.println("Bye!");
        }

If there is any faster method please message me, and thanks in advance.

El dimecres, 13 juliol de 2016 10:40:06 UTC+2, Oscar themacboy va escriure:

Oscar themacboy

unread,
Jul 13, 2016, 6:11:20 AM7/13/16
to BSON
opsss forget to mention the method:

BsonType.getBsonType();

they you can switch values.
Reply all
Reply to author
Forward
0 new messages