CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-mongodb--mongo-java-driver

MongoDB Java Driver legacy all-in-one JAR providing comprehensive database connectivity for Java applications with synchronous APIs, CRUD operations, aggregation, GridFS, change streams, and authentication support

Pending
Overview
Eval results
Files

bson-types.mddocs/

BSON Data Types

Comprehensive BSON document and type system providing both flexible document APIs and strongly-typed value representations for MongoDB data interchange.

Capabilities

Document Class

Flexible document representation extending LinkedHashMap for easy MongoDB document manipulation.

/**
 * BSON document representation extending Map<String, Object>
 */
public class Document extends LinkedHashMap<String, Object> {
    // Constructors
    public Document();
    public Document(String key, Object value);
    public Document(Map<String, Object> map);
    
    // Fluent building
    public Document append(String key, Object value);
    
    // JSON conversion
    public String toJson();
    public String toJson(JsonWriterSettings settings);
    public static Document parse(String json);
    public static Document parse(String json, Codec<Document> codec);
    
    // Type-safe field access
    public Object get(Object key);
    public <T> T get(Object key, Class<T> clazz);
    public String getString(Object key);
    public Integer getInteger(Object key);
    public Long getLong(Object key);
    public Double getDouble(Object key);
    public Boolean getBoolean(Object key);
    public Date getDate(Object key);
    public ObjectId getObjectId(Object key);
    public List<Object> getList(Object key, Class<?> clazz);
}

Document Usage Examples:

// Create document
Document user = new Document("name", "Alice")
    .append("age", 30)
    .append("email", "alice@example.com")
    .append("active", true);

// Nested document
Document address = new Document("street", "123 Main St")
    .append("city", "Springfield")
    .append("zipCode", "12345");
user.append("address", address);

// Array field
user.append("hobbies", Arrays.asList("reading", "swimming", "cooking"));

// Convert to JSON
String json = user.toJson();
System.out.println(json);

// Parse from JSON
Document parsed = Document.parse(json);

// Type-safe field access
String name = user.getString("name");
Integer age = user.getInteger("age");
Document userAddress = user.get("address", Document.class);

BsonDocument Class

Type-safe BSON document with strongly-typed field access and BSON value hierarchy integration.

/**
 * Type-safe BSON document implementation
 */
public class BsonDocument extends BsonValue implements Map<String, BsonValue>, Cloneable {
    // Constructors
    public BsonDocument();
    public BsonDocument(String key, BsonValue value);
    public BsonDocument(List<BsonElement> bsonElements);
    
    // Fluent building
    public BsonDocument append(String key, BsonValue value);
    
    // Map operations
    public BsonValue get(Object key);
    public BsonValue put(String key, BsonValue value);
    public Set<String> keySet();
    public Collection<BsonValue> values();
    public Set<Entry<String, BsonValue>> entrySet();
    
    // Utility methods
    public String getFirstKey();
    public boolean isEmpty();
    public int size();
    public BsonDocument clone();
    
    // Type checking from BsonValue
    public BsonType getBsonType(); // Returns DOCUMENT
    public boolean isDocument();
    public BsonDocument asDocument();
}

BsonValue Hierarchy

Abstract base class for all strongly-typed BSON values with type checking and conversion methods.

/**
 * Abstract base class for all BSON values
 */
public abstract class BsonValue {
    public abstract BsonType getBsonType();
    
    // Type checking methods
    public boolean isArray();
    public boolean isBinary();
    public boolean isBoolean();
    public boolean isDateTime();
    public boolean isDocument();
    public boolean isDouble();
    public boolean isInt32();
    public boolean isInt64();
    public boolean isNull();
    public boolean isNumber();
    public boolean isObjectId();
    public boolean isString();
    public boolean isTimestamp();
    
    // Type conversion methods  
    public BsonArray asArray();
    public BsonBinary asBinary();
    public BsonBoolean asBoolean();
    public BsonDateTime asDateTime();
    public BsonDocument asDocument();
    public BsonDouble asDouble();
    public BsonInt32 asInt32();
    public BsonInt64 asInt64();
    public BsonNull asNull();
    public BsonNumber asNumber();
    public BsonObjectId asObjectId();
    public BsonString asString();
    public BsonTimestamp asTimestamp();
}

Primitive BSON Types

Strongly-typed BSON primitive value implementations.

/**
 * BSON string value
 */
public class BsonString extends BsonValue {
    public BsonString(String value);
    public String getValue();
    public BsonType getBsonType(); // Returns STRING
}

/**
 * BSON 32-bit integer value
 */
public class BsonInt32 extends BsonNumber {
    public BsonInt32(int value);
    public int getValue();
    public BsonType getBsonType(); // Returns INT32
}

/**
 * BSON 64-bit integer value
 */
public class BsonInt64 extends BsonNumber {
    public BsonInt64(long value);
    public long getValue();
    public BsonType getBsonType(); // Returns INT64
}

/**
 * BSON double-precision floating point value
 */
public class BsonDouble extends BsonNumber {
    public BsonDouble(double value);
    public double getValue();
    public BsonType getBsonType(); // Returns DOUBLE
}

/**
 * BSON boolean value
 */
public class BsonBoolean extends BsonValue {
    public static final BsonBoolean TRUE;
    public static final BsonBoolean FALSE;
    
    public BsonBoolean(boolean value);
    public boolean getValue();
    public BsonType getBsonType(); // Returns BOOLEAN
}

/**
 * BSON null value
 */
public class BsonNull extends BsonValue {
    public static final BsonNull VALUE;
    
    public BsonType getBsonType(); // Returns NULL
}

Complex BSON Types

BSON array, binary data, and specialized types.

/**
 * BSON array value implementing List<BsonValue>
 */
public class BsonArray extends BsonValue implements List<BsonValue>, Cloneable {
    public BsonArray();
    public BsonArray(List<? extends BsonValue> list);
    
    // List interface methods
    public boolean add(BsonValue bsonValue);
    public void add(int index, BsonValue element);
    public BsonValue get(int index);
    public BsonValue set(int index, BsonValue element);
    public int size();
    public boolean isEmpty();
    
    // Type-specific methods
    public BsonType getBsonType(); // Returns ARRAY
    public BsonArray clone();
}

/**
 * BSON binary data value
 */
public class BsonBinary extends BsonValue {
    public BsonBinary(byte[] data);
    public BsonBinary(BsonBinarySubType type, byte[] data);
    
    public byte[] getData();
    public BsonBinarySubType getType();
    public BsonType getBsonType(); // Returns BINARY
}

/**
 * BSON ObjectId value
 */
public class BsonObjectId extends BsonValue implements Comparable<BsonObjectId> {
    public BsonObjectId();
    public BsonObjectId(ObjectId value);
    
    public ObjectId getValue();
    public BsonType getBsonType(); // Returns OBJECT_ID
    public int compareTo(BsonObjectId other);
}

/**
 * BSON date/time value
 */
public class BsonDateTime extends BsonValue {
    public BsonDateTime(long value);
    public BsonDateTime(Date date);
    
    public long getValue(); // Milliseconds since Unix epoch
    public BsonType getBsonType(); // Returns DATE_TIME
}

/**
 * BSON regular expression value
 */
public class BsonRegularExpression extends BsonValue {
    public BsonRegularExpression(String pattern);
    public BsonRegularExpression(String pattern, String options);
    
    public String getPattern();
    public String getOptions();
    public BsonType getBsonType(); // Returns REGULAR_EXPRESSION
}

ObjectId

MongoDB's unique identifier type with timestamp and uniqueness guarantees.

/**
 * MongoDB ObjectId - 12-byte unique identifier
 */
public class ObjectId implements Comparable<ObjectId>, Serializable {
    // Constructors
    public ObjectId();
    public ObjectId(Date date);
    public ObjectId(String hexString);
    public ObjectId(byte[] bytes);
    public ObjectId(int timestamp, int randomValue1, short randomValue2, int counter);
    
    // String conversion
    public String toHexString();
    public static boolean isValid(String hexString);
    
    // Timestamp extraction
    public Date getDate();
    public int getTimestamp();
    
    // Comparison and equality
    public int compareTo(ObjectId other);
    public boolean equals(Object obj);
    public int hashCode();
    
    // Byte representation
    public byte[] toByteArray();
}

ObjectId Usage Examples:

// Generate new ObjectId
ObjectId id = new ObjectId();
System.out.println("Generated ID: " + id.toHexString());

// Create from string
ObjectId parsed = new ObjectId("507f1f77bcf86cd799439011");

// Extract timestamp
Date creationTime = id.getDate();
System.out.println("Created at: " + creationTime);

// Validate ObjectId string
if (ObjectId.isValid("507f1f77bcf86cd799439011")) {
    ObjectId valid = new ObjectId("507f1f77bcf86cd799439011");
}

BsonType Enumeration

Enumeration of all BSON data types for type checking and switching.

/**
 * Enumeration of BSON types
 */
public enum BsonType {
    END_OF_DOCUMENT(0),
    DOUBLE(1),
    STRING(2),
    DOCUMENT(3),
    ARRAY(4),
    BINARY(5),
    UNDEFINED(6),    // Deprecated
    OBJECT_ID(7),
    BOOLEAN(8),
    DATE_TIME(9),
    NULL(10),
    REGULAR_EXPRESSION(11),
    DB_POINTER(12),  // Deprecated
    JAVASCRIPT(13),
    SYMBOL(14),      // Deprecated  
    JAVASCRIPT_WITH_SCOPE(15), // Deprecated
    INT32(16),
    TIMESTAMP(17),
    INT64(18),
    DECIMAL128(19),
    MIN_KEY(-1),
    MAX_KEY(127);
    
    public int getValue();
    public static BsonType findByValue(int value);
}

Codec System Integration

Integration with MongoDB's codec system for custom serialization.

/**
 * Codec for Document class
 */
public class DocumentCodec implements CollectibleCodec<Document> {
    public DocumentCodec();
    public DocumentCodec(BsonTypeClassMap bsonTypeClassMap);
    public DocumentCodec(CodecRegistry registry, BsonTypeClassMap bsonTypeClassMap);
    
    public void encode(BsonWriter writer, Document document, EncoderContext encoderContext);
    public Document decode(BsonReader reader, DecoderContext decoderContext);
    public Class<Document> getEncoderClass();
}

/**
 * Codec for BsonDocument class
 */
public class BsonDocumentCodec implements Codec<BsonDocument> {
    public static final BsonDocumentCodec DEFAULT;
    
    public void encode(BsonWriter writer, BsonDocument document, EncoderContext encoderContext);
    public BsonDocument decode(BsonReader reader, DecoderContext decoderContext);
    public Class<BsonDocument> getEncoderClass();
}

Custom Codec Example:

// Create custom codec registry
CodecRegistry defaultCodecRegistry = MongoClientSettings.getDefaultCodecRegistry();
CodecRegistry customCodecRegistry = fromRegistries(
    defaultCodecRegistry,
    fromCodecs(new MyCustomCodec())
);

// Use with collection
MongoCollection<MyCustomType> collection = database
    .getCollection("mycollection", MyCustomType.class)
    .withCodecRegistry(customCodecRegistry);

Types

/**
 * Element in a BSON document (key-value pair)
 */
public class BsonElement {
    public BsonElement(String name, BsonValue value);
    
    public String getName();
    public BsonValue getValue();
}

/**
 * Binary data subtype enumeration
 */
public enum BsonBinarySubType {
    BINARY(0),
    FUNCTION(1),
    BINARY_OLD(2),   // Deprecated
    UUID_LEGACY(3),  // Deprecated  
    UUID_STANDARD(4),
    MD5(5),
    ENCRYPTED(6),
    USER_DEFINED(128);
    
    public byte getValue();
    public static BsonBinarySubType forValue(byte value);
}

/**
 * BSON timestamp type for MongoDB internal use
 */
public class BsonTimestamp extends BsonValue implements Comparable<BsonTimestamp> {
    public BsonTimestamp();
    public BsonTimestamp(int time, int inc);
    public BsonTimestamp(long value);
    
    public int getTime();
    public int getInc();
    public long getValue();
    public BsonType getBsonType(); // Returns TIMESTAMP
}

Install with Tessl CLI

npx tessl i tessl/maven-org-mongodb--mongo-java-driver

docs

aggregation.md

authentication-security.md

bson-types.md

change-streams.md

collection-operations.md

configuration.md

connection-management.md

database-operations.md

gridfs.md

index.md

query-building.md

sessions-transactions.md

tile.json