The BSONUtil class

When accessing BSON objects, you may find it handy to use the BSONUtil class, which provides a number of convenient methods.

Accessing BSONUtil

You can access this class by adding the following line of code to your filter:

const BSONUtil = Java.type("com.galliumdata.server.handler.mongo.BSONUtil");

The name of the constant is up to you, of course, but the examples in this documentation will use the name BSONUtil.


object decodeBSON(byte[] bytes)

This is useful when you have a byte array that needs to be decoded, which is not unusual with MongoDB. It returns an object that will behave like a JavaScript object.

byte[] encodeBSON(object obj)

Encodes the given object into a byte array.

int getObjectSize(object obj)

Returns the size, in bytes, of the given BSON object.

boolean bsonHasProperty(object obj, String path)

Returns true if the given BSON object defines a property corresponding to the given path, which is one or more property name divided by periods, e.g. address[0].street.number

object getBsonProperty(object obj, String path)

Returns the value of the property designated by the path, which is a JSON Path-like expression, e.g. attr1.attr2[0].attr3

boolean bsonValueEquals(BsonValue bsonValue, Object val)

Returns true if the BSON value is equal to the given value, as best as we can tell. BSON has more types than JSON or JavaScript, which sometimes makes this method useful.