Skip to main content

Type Support

The table recommends Java property types for common BSON types. See Java/JDBC types.

Type Mappings

Vector field usage is described under Vector Types.

BSON typeJava typeDescription
BSON stringStringRead text by field name or entity property mapping.
BSON int32IntegerSigned 32-bit integer.
BSON int64LongSigned 64-bit integer.
BSON doubleDouble64-bit floating-point value.
BSON booleanBooleanUse Boolean semantics, not a general integer property.
BSON dateInstant / java.sql.TimestampUse when retaining the instant; choose java.sql.Date only for date-only business values.
BSON documentMap / BeanSee JSON Field Mapping.
BSON arrayListSee JSON Field Mapping.
BSON stringEnumStore enum names; declare the concrete enum class and keep field values aligned with constant names.
BSON binarybyte[]Binary content without character encoding conversion.
BSON ObjectIdStringStore the hexadecimal string in the entity; use ObjectId(?) in commands.

A BSON date stores an instant; a java.sql.Date target represents only the date. Use Timestamp or Instant when the time of day matters. Document and array content must match the configured object or list mapping.

Vector Types

See Vector Operations for field mapping and reads/writes.

Example: Bind a Boolean and Read Its Field

jdbcTemplate.execute("use test");
jdbcTemplate.execute("db.createCollection('type_example')");
jdbcTemplate.executeUpdate("test.type_example.insert({id: ?, enabled: ?})",
new Object[] { 1, true });
Boolean enabled = jdbcTemplate.queryForObject(
"test.type_example.find({id: ?}, {enabled: 1})",
new Object[] { 1 }, (rs, rowNum) -> rs.getBoolean("enabled"));

Limits

  • Default mapping does not preserve BigInteger or exact decimals losslessly.
  • The first column is document metadata; read business fields by name, RowMapper, or entity.

Array Types

BSON arrays can be bound and read through the adapter’s JDBC ARRAY support. Use a homogeneous Integer[], Float[], or String[] property; document arrays with nested objects can use JSON mapping.

Use an empty array_example collection in the test database:

import java.sql.Types;
import net.hasor.dbvisitor.types.SqlArg;

Integer[] values = { 10, 20, 30 };
jdbc.executeUpdate("test.array_example.insert({id: ?, int_array: ?})",
new Object[] { 1, SqlArg.valueOf(values, Types.ARRAY) });
Integer[] loaded = jdbc.queryForObject(
"test.array_example.find({id: ?}, {_id: 0, int_array: 1})",
new Object[] { 1 }, Integer[].class);