CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-mongodb--bson

The BSON library for MongoDB Java Driver - A high-performance binary serialization format and library for MongoDB documents

Pending
Overview
Eval results
Files

types.mddocs/

Specialized Data Types

The BSON library provides specialized data types for MongoDB-specific values and advanced data modeling. These types handle unique identifiers, high-precision decimals, binary data, timestamps, and other specialized BSON types that extend beyond basic JSON capabilities.

ObjectId

The ObjectId class represents MongoDB's unique identifier type, which is a 12-byte identifier consisting of a timestamp, machine identifier, process identifier, and counter.

public final class ObjectId implements Comparable<ObjectId>, Serializable {
    /**
     * Constructs a new instance using the current time, machine identifier, process identifier, and a counter.
     */
    public ObjectId();
    
    /**
     * Constructs a new instance from the given byte array.
     * @param bytes the byte array
     * @throws IllegalArgumentException if the byte array is not of length 12
     */
    public ObjectId(byte[] bytes);
    
    /**
     * Constructs a new instance from the given ByteBuffer.
     * @param buffer the ByteBuffer
     * @throws IllegalArgumentException if the buffer does not have 12 remaining bytes
     */
    public ObjectId(ByteBuffer buffer);
    
    /**
     * Constructs a new instance from a 24-character hexadecimal string representation.
     * @param hexString the string to convert
     * @throws IllegalArgumentException if the string is not a valid hex string representation of an ObjectId
     */
    public ObjectId(String hexString);
    
    /**
     * Constructs a new instance using the given date.
     * @param date the date
     */
    public ObjectId(Date date);
    
    /**
     * Constructs a new instance using the given date and counter.
     * @param date the date
     * @param counter the counter
     * @throws IllegalArgumentException if the high order byte of counter is not 0
     */
    public ObjectId(Date date, int counter);
    
    /**
     * Constructs a new instance.
     * @param timestamp the timestamp (seconds since epoch)
     * @param machineAndProcessIdentifier the machine and process identifier
     * @param counter the counter
     * @throws IllegalArgumentException if the high order byte of counter is not 0
     */
    public ObjectId(int timestamp, int machineAndProcessIdentifier, int counter);
    
    /**
     * Gets the timestamp (seconds since epoch).
     * @return the timestamp
     */
    public int getTimestamp();
    
    /**
     * Gets the machine identifier.
     * @return the machine identifier
     */
    public int getMachineIdentifier();
    
    /**
     * Gets the process identifier.
     * @return the process identifier
     */
    public int getProcessIdentifier();
    
    /**
     * Gets the counter.
     * @return the counter
     */
    public int getCounter();
    
    /**
     * Gets the timestamp as a Date instance.
     * @return the Date
     */
    public Date getDate();
    
    /**
     * Converts this instance to a 24-character hexadecimal string representation.
     * @return the hexadecimal string representation
     */
    public String toHexString();
    
    /**
     * Convert to a byte array. Note that the numbers are stored in big-endian order.
     * @return the byte array
     */
    public byte[] toByteArray();
    
    /**
     * Gets a new ObjectId with the current date/time.
     * @return the ObjectId
     */
    public static ObjectId get();
    
    /**
     * Checks if a string could be an ObjectId.
     * @param hexString a potential ObjectId as a String
     * @return whether the string could be an object id
     * @throws IllegalArgumentException if hexString is null
     */
    public static boolean isValid(String hexString);
    
    /**
     * Gets the generated machine identifier.
     * @return an int representing the machine identifier
     */
    public static int getGeneratedMachineIdentifier();
    
    /**
     * Gets the current process identifier.
     * @return the process id
     */
    public static int getCurrentProcessId();
    
    public int compareTo(ObjectId other);
    public boolean equals(Object o);
    public int hashCode();
    public String toString();
}

Decimal128

The Decimal128 class represents a 128-bit decimal floating point value, which provides exact decimal representation for financial and monetary calculations.

public final class Decimal128 implements Comparable<Decimal128> {
    /**
     * A constant holding the positive infinity of type Decimal128.
     */
    public static final Decimal128 POSITIVE_INFINITY;
    
    /**
     * A constant holding the negative infinity of type Decimal128.
     */
    public static final Decimal128 NEGATIVE_INFINITY;
    
    /**
     * A constant holding a Not-a-Number (NaN) value of type Decimal128.
     */
    public static final Decimal128 NaN;
    
    /**
     * A constant holding the positive zero value of type Decimal128.
     */
    public static final Decimal128 POSITIVE_ZERO;
    
    /**
     * A constant holding the negative zero value of type Decimal128.
     */
    public static final Decimal128 NEGATIVE_ZERO;
    
    /**
     * Create an instance from the given high and low order bits.
     * @param high the high order 64 bits
     * @param low the low order 64 bits
     */
    public Decimal128(long high, long low);
    
    /**
     * Returns an instance representing the given long value.
     * @param value the long value
     * @return the Decimal128 instance
     */
    public static Decimal128 fromIEEE754BIDEncoding(long high, long low);
    
    /**
     * Create a Decimal128 instance from a BigDecimal.
     * @param value the BigDecimal value
     * @return the Decimal128 instance
     */
    public static Decimal128 parse(String value);
    
    /**
     * Create a Decimal128 instance from a BigDecimal.
     * @param bigDecimalValue the BigDecimal value
     * @return the Decimal128 instance
     */
    public static Decimal128 valueOf(BigDecimal bigDecimalValue);
    
    /**
     * Create a Decimal128 instance from a long.
     * @param longValue the long value
     * @return the Decimal128 instance
     */
    public static Decimal128 valueOf(long longValue);
    
    /**
     * Create a Decimal128 instance from a double.
     * @param doubleValue the double value
     * @return the Decimal128 instance
     */
    public static Decimal128 valueOf(double doubleValue);
    
    /**
     * Create a Decimal128 instance from a string.
     * @param value the string value
     * @return the Decimal128 instance
     */
    public static Decimal128 valueOf(String value);
    
    /**
     * Returns true if this Decimal128 is finite.
     * @return true if finite
     */
    public boolean isFinite();
    
    /**
     * Returns true if this Decimal128 is infinite.
     * @return true if infinite
     */
    public boolean isInfinite();
    
    /**
     * Returns true if this Decimal128 is Not-A-Number (NaN).
     * @return true if NaN
     */
    public boolean isNaN();
    
    /**
     * Returns true if this Decimal128 is negative.
     * @return true if negative
     */
    public boolean isNegative();
    
    /**
     * Gets the high order 64 bits.
     * @return the high order 64 bits
     */
    public long getHigh();
    
    /**
     * Gets the low order 64 bits.
     * @return the low order 64 bits
     */
    public long getLow();
    
    /**
     * Returns this Decimal128 as a BigDecimal.
     * @return the BigDecimal representation
     */
    public BigDecimal bigDecimalValue();
    
    /**
     * Returns this Decimal128 as a double.
     * @return the double representation
     */
    public double doubleValue();
    
    public int compareTo(Decimal128 o);
    public boolean equals(Object o);
    public int hashCode();
    public String toString();
}

Binary Data Types

BsonBinary

public final class BsonBinary extends BsonValue {
    /**
     * Constructs a new instance.
     * @param data the binary data
     */
    public BsonBinary(byte[] data);
    
    /**
     * Constructs a new instance.
     * @param type the binary sub type
     * @param data the binary data
     */
    public BsonBinary(BsonBinarySubType type, byte[] data);
    
    /**
     * Constructs a new instance with a UUID.
     * @param uuid the UUID
     */
    public BsonBinary(UUID uuid);
    
    /**
     * Constructs a new instance with a UUID and representation.
     * @param uuid the UUID
     * @param uuidRepresentation the UUID representation
     */
    public BsonBinary(UUID uuid, UuidRepresentation uuidRepresentation);
    
    /**
     * Gets the type of this binary.
     * @return the type
     */
    public byte getType();
    
    /**
     * Gets the binary sub type.
     * @return the binary sub type
     */
    public BsonBinarySubType getBsonBinarySubType();
    
    /**
     * Gets the binary data.
     * @return the binary data
     */
    public byte[] getData();
    
    /**
     * Gets this binary as a UUID.
     * @return the UUID
     * @throws BsonInvalidOperationException if this binary is not a UUID
     */
    public UUID asUuid();
    
    /**
     * Gets this binary as a UUID with the given representation.
     * @param uuidRepresentation the UUID representation
     * @return the UUID
     */
    public UUID asUuid(UuidRepresentation uuidRepresentation);
}

BsonBinarySubType

public enum BsonBinarySubType {
    /**
     * Generic binary sub type.
     */
    BINARY(0x00),
    
    /**
     * Function binary sub type.
     */
    FUNCTION(0x01),
    
    /**
     * Old binary sub type.
     */
    OLD_BINARY(0x02),
    
    /**
     * UUID old sub type.
     */
    UUID_LEGACY(0x03),
    
    /**
     * UUID standard sub type.
     */
    UUID_STANDARD(0x04),
    
    /**
     * MD5 sub type.
     */
    MD5(0x05),
    
    /**
     * Encrypted sub type.
     */
    ENCRYPTED(0x06),
    
    /**
     * Column sub type.
     */
    COLUMN(0x07),
    
    /**
     * User defined sub type.
     */
    USER_DEFINED(0x80);
    
    /**
     * Gets the value of this sub type.
     * @return the value
     */
    public byte getValue();
    
    /**
     * Returns true if the given value is a UUID sub type.
     * @param value the value
     * @return true if UUID sub type
     */
    public static boolean isUuid(byte value);
}

Timestamp Types

BsonTimestamp

public final class BsonTimestamp extends BsonValue implements Comparable<BsonTimestamp> {
    /**
     * Constructs a new instance.
     * @param seconds the seconds since epoch
     * @param increment the increment
     */
    public BsonTimestamp(int seconds, int increment);
    
    /**
     * Constructs a new instance.
     * @param value the combined timestamp value
     */
    public BsonTimestamp(long value);
    
    /**
     * Gets the time in seconds since epoch.
     * @return the time in seconds since epoch
     */
    public int getTime();
    
    /**
     * Gets the increment.
     * @return the increment
     */
    public int getInc();
    
    /**
     * Gets the combined timestamp value.
     * @return the combined timestamp value
     */
    public long getValue();
    
    public int compareTo(BsonTimestamp ts);
}

BsonDateTime

public final class BsonDateTime extends BsonValue implements Comparable<BsonDateTime> {
    /**
     * Constructs a new instance.
     * @param value the date time value as milliseconds since epoch
     */
    public BsonDateTime(long value);
    
    /**
     * Gets the DateTime value as milliseconds since epoch.
     * @return the DateTime value
     */
    public long getValue();
    
    public int compareTo(BsonDateTime o);
}

Regular Expressions

public final class BsonRegularExpression extends BsonValue {
    /**
     * Constructs a new instance.
     * @param pattern the pattern
     */
    public BsonRegularExpression(String pattern);
    
    /**
     * Constructs a new instance.
     * @param pattern the pattern
     * @param options the options
     */
    public BsonRegularExpression(String pattern, String options);
    
    /**
     * Gets the pattern.
     * @return the pattern
     */
    public String getPattern();
    
    /**
     * Gets the options.
     * @return the options
     */
    public String getOptions();
}

JavaScript Types

BsonJavaScript

public final class BsonJavaScript extends BsonValue {
    /**
     * Constructs a new instance.
     * @param code the JavaScript code
     */
    public BsonJavaScript(String code);
    
    /**
     * Gets the JavaScript code.
     * @return the code
     */
    public String getCode();
}

BsonJavaScriptWithScope

public final class BsonJavaScriptWithScope extends BsonValue {
    /**
     * Constructs a new instance.
     * @param code the JavaScript code
     * @param scope the scope document
     */
    public BsonJavaScriptWithScope(String code, BsonDocument scope);
    
    /**
     * Gets the JavaScript code.
     * @return the code
     */
    public String getCode();
    
    /**
     * Gets the scope document.
     * @return the scope
     */
    public BsonDocument getScope();
}

Database Pointer (Deprecated)

public final class BsonDbPointer extends BsonValue {
    /**
     * Constructs a new instance.
     * @param namespace the namespace
     * @param id the id
     */
    public BsonDbPointer(String namespace, ObjectId id);
    
    /**
     * Gets the namespace.
     * @return the namespace
     */
    public String getNamespace();
    
    /**
     * Gets the id.
     * @return the id
     */
    public ObjectId getId();
}

Symbol (Deprecated)

public final class BsonSymbol extends BsonValue {
    /**
     * Constructs a new instance.
     * @param symbol the symbol
     */
    public BsonSymbol(String symbol);
    
    /**
     * Gets the symbol value.
     * @return the symbol
     */
    public String getSymbol();
}

Binary Vector Types

BinaryVector Interface

public sealed interface BinaryVector permits Float32BinaryVector, Int8BinaryVector, PackedBitBinaryVector {
    /**
     * Returns the data type of the vector.
     * @return the data type
     */
    BsonBinarySubType getType();
    
    /**
     * Returns the binary representation of the vector.
     * @return the binary data
     */
    byte[] getData();
    
    /**
     * Returns the padding used for this vector.
     * @return the padding
     */
    byte getPadding();
}

Float32BinaryVector

public final class Float32BinaryVector implements BinaryVector {
    /**
     * Constructs a Float32BinaryVector.
     * @param values the vector values
     */
    public Float32BinaryVector(List<Float> values);
    
    /**
     * Gets the vector values.
     * @return the vector values
     */
    public List<Float> getValues();
    
    public BsonBinarySubType getType();
    public byte[] getData();
    public byte getPadding();
}

Int8BinaryVector

public final class Int8BinaryVector implements BinaryVector {
    /**
     * Constructs an Int8BinaryVector.
     * @param values the vector values
     */
    public Int8BinaryVector(List<Byte> values);
    
    /**
     * Gets the vector values.
     * @return the vector values
     */
    public List<Byte> getValues();
    
    public BsonBinarySubType getType();
    public byte[] getData();
    public byte getPadding();
}

PackedBitBinaryVector

public final class PackedBitBinaryVector implements BinaryVector {
    /**
     * Constructs a PackedBitBinaryVector.
     * @param values the bit values (0 or 1)
     * @param padding the padding for the last byte
     */
    public PackedBitBinaryVector(List<Byte> values, byte padding);
    
    /**
     * Gets the bit values.
     * @return the bit values
     */
    public List<Byte> getValues();
    
    public BsonBinarySubType getType();
    public byte[] getData();
    public byte getPadding();
}

Code Types (Legacy)

Code

public class Code {
    /**
     * Constructs a new instance.
     * @param code the code
     */
    public Code(String code);
    
    /**
     * Gets the code.
     * @return the code
     */
    public String getCode();
    
    public boolean equals(Object o);
    public int hashCode();
    public String toString();
}

CodeWithScope

public class CodeWithScope extends Code {
    /**
     * Constructs a new instance.
     * @param code the code
     * @param scope the scope
     */
    public CodeWithScope(String code, Object scope);
    
    /**
     * Gets the scope.
     * @return the scope
     */
    public Object getScope();
    
    public boolean equals(Object o);
    public int hashCode();
    public String toString();
}

Usage Examples

Working with ObjectId

import org.bson.types.ObjectId;
import java.util.Date;

// Create ObjectIds
ObjectId id1 = new ObjectId(); // Current time
ObjectId id2 = new ObjectId("507f1f77bcf86cd799439011"); // From hex string
ObjectId id3 = new ObjectId(new Date()); // From specific date

// Extract information
Date creationTime = id1.getDate();
int timestamp = id1.getTimestamp(); // Seconds since epoch
String hex = id1.toHexString();
byte[] bytes = id1.toByteArray();

// Validate ObjectId strings
boolean valid = ObjectId.isValid("507f1f77bcf86cd799439011"); // true
boolean invalid = ObjectId.isValid("not-an-objectid"); // false

// Compare ObjectIds (chronological order)
ObjectId earlier = new ObjectId(new Date(System.currentTimeMillis() - 10000));
ObjectId later = new ObjectId();
int comparison = earlier.compareTo(later); // negative value

High-Precision Decimal Operations

import org.bson.types.Decimal128;
import java.math.BigDecimal;

// Create Decimal128 values
Decimal128 price = Decimal128.parse("99.99");
Decimal128 tax = Decimal128.valueOf(new BigDecimal("8.25"));
Decimal128 discount = Decimal128.valueOf(5.0);

// Convert to other types
BigDecimal priceBD = price.bigDecimalValue();
double priceDouble = price.doubleValue(); // May lose precision

// Check special values
Decimal128 infinity = Decimal128.POSITIVE_INFINITY;
Decimal128 nan = Decimal128.NaN;
boolean isFinite = price.isFinite(); // true
boolean isNegative = discount.isNegative(); // false

// Use in calculations (via BigDecimal)
BigDecimal total = priceBD.add(tax.bigDecimalValue()).subtract(discount.bigDecimalValue());
Decimal128 totalDecimal = Decimal128.valueOf(total);

Binary Data Handling

import org.bson.*;
import java.util.UUID;

// Generic binary data
byte[] imageData = loadImageBytes();
BsonBinary imageBinary = new BsonBinary(BsonBinarySubType.GENERIC, imageData);

// UUID handling
UUID uuid = UUID.randomUUID();
BsonBinary uuidBinary = new BsonBinary(uuid);
UUID retrievedUuid = uuidBinary.asUuid();

// Different UUID representations
BsonBinary javaLegacyUuid = new BsonBinary(uuid, UuidRepresentation.JAVA_LEGACY);
BsonBinary standardUuid = new BsonBinary(uuid, UuidRepresentation.STANDARD);

// Working with binary vectors
List<Float> vectorData = Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f);
Float32BinaryVector vector = new Float32BinaryVector(vectorData);
BsonBinary vectorBinary = new BsonBinary(BsonBinarySubType.VECTOR, vector.getData());

Timestamp Operations

import org.bson.*;

// BSON Timestamp (MongoDB internal)
BsonTimestamp oplogTimestamp = new BsonTimestamp(1609459200, 1); // seconds, increment
int seconds = oplogTimestamp.getTime();
int increment = oplogTimestamp.getInc();

// BSON DateTime (standard date/time)
long currentMillis = System.currentTimeMillis();
BsonDateTime dateTime = new BsonDateTime(currentMillis);
long retrievedMillis = dateTime.getValue();

// Compare timestamps
BsonTimestamp ts1 = new BsonTimestamp(1609459200, 1);
BsonTimestamp ts2 = new BsonTimestamp(1609459200, 2);
int comparison = ts1.compareTo(ts2); // negative (ts1 is earlier)

Regular Expressions

import org.bson.BsonRegularExpression;
import java.util.regex.Pattern;

// Create BSON regular expressions
BsonRegularExpression regex1 = new BsonRegularExpression("^hello.*world$");
BsonRegularExpression regex2 = new BsonRegularExpression("email@domain", "i");

String pattern = regex1.getPattern(); // "^hello.*world$"
String options = regex1.getOptions(); // ""
String caseInsensitive = regex2.getOptions(); // "i"

// Use in documents
BsonDocument query = new BsonDocument("email", regex2);

JavaScript Code Storage

import org.bson.*;

// Simple JavaScript code
BsonJavaScript mapFunction = new BsonJavaScript(
    "function() { emit(this.category, this.amount); }"
);

// JavaScript with scope
BsonDocument scope = new BsonDocument()
    .append("multiplier", new BsonInt32(10))
    .append("threshold", new BsonDouble(100.0));

BsonJavaScriptWithScope reduceFunction = new BsonJavaScriptWithScope(
    "function(key, values) { return Array.sum(values) * multiplier; }",
    scope
);

String code = reduceFunction.getCode();
BsonDocument retrievedScope = reduceFunction.getScope();

Install with Tessl CLI

npx tessl i tessl/maven-org-mongodb--bson

docs

codecs.md

core-types.md

index.md

io.md

json.md

legacy-api.md

types.md

tile.json