You are currently viewing an older version of the Java driver documentation.
For the most recent version of the reference documentation, see our MongoDB Java Driver documentation site.
  • What's New

What’s New in 3.6

Key new features of the 3.6 Java driver release:

Change Stream support

The 3.6 release adds support for change streams.

Retryable writes

The 3.6 release adds support for retryable writes using the retryWrites option in MongoClientOptions.

Compression

The 3.6 release adds support for compression of messages to and from appropriately configured MongoDB servers:

Causal consistency

The 3.6 release adds support for causally consistency via the new ClientSession API.

Application-configured server selection

The 3.6 release adds support for application-configured control over server selection, using the serverSelector option in MongoClientOptions.

PojoCodec improvements

The 3.6 release brings new improvements to the PojoCodec:

  • Improved sub-class and discriminator support.
  • Support for custom Collection and Map implementations.
  • Improvements to the BsonCreator annotation, which now supports @BsonId and @BsonProperty with values that represent the read name of the property.
  • A new PropertyCodecProvider API, allowing for easy and type-safe handling of container types.
  • Added the SET_PRIVATE_FIELDS_CONVENTION convention.
  • Added the USE_GETTERS_FOR_SETTERS convention.

The MongoDB Java drivers team would like to thank both Joseph Florencio and Qi Liu for their excellent contributions to the PojoCodec.

What’s New in 3.5

Key new features of the 3.5 Java driver release:

Native POJO support

The 3.5 release adds support for POJO serialization at the BSON layer, and can be used by the synchronous and asynchronous drivers. See the POJO Quick start pages for details.

Improved JSON support

The 3.5 release improves support for JSON parsing and generation.

  • Implements the new Extended JSON specification
  • Implements custom JSON converters to give applications full control over JSON generation for each BSON type

See the JSON reference for details.

Connection pool monitoring

The 3.5 release adds support for monitoring connection pool-related events.

SSLContext configuration

The 3.5 release supports overriding the default javax.net.ssl.SSLContext used for SSL connections to MongoDB.

KeepAlive configuration deprecated

The 3.5 release deprecated socket keep-alive settings, also socket keep-alive checks are now on by default. It is strongly recommended that system keep-alive settings should be configured with shorter timeouts.

See the ‘does TCP keep-alive time affect MongoDB deployments?’ documentation for more information.

What’s New in 3.4

The 3.4 release includes full support for the MongoDB 3.4 server release. Key new features include:

Support for Decimal128 Format

import org.bson.types.Decimal128;

The Decimal128 format supports numbers with up to 34 decimal digits (i.e. significant digits) and an exponent range of −6143 to +6144.

To create a Decimal128 number, you can use

Support for Collation

import com.mongodb.client.model.Collation;

Collation allows users to specify language-specific rules for string comparison. Use the Collation.builder() to create the Collation object. For example, the following example creates a Collation object with Primary level of comparison and locale fr.

Collation.builder().collationStrength(CollationStrength.PRIMARY).locale("fr").build()));

You can specify collation at the collection level, at an index level, or at a collation-supported operation level:

Collection Level

To specify collation at the collection level, pass a Collation object as an option to the createCollection() method. To specify options to the createCollection method, use the CreateCollectionOptions class.

database.createCollection("myColl", new CreateCollectionOptions().collation(
                              Collation.builder()
                                    .collationStrength(CollationStrength.PRIMARY)
                                    .locale("fr").build()));

Index Level

To specify collation for an index, pass a Collation object as an option to the createIndex() method. To specify index options, use the IndexOptions class.

IndexOptions collationIndexOptions = new IndexOptions().name("collation-fr")
                                       .collation(Collation.builder()
                                       .collationStrength(CollationStrength.SECONDARY)
                                       .locale("fr").build());

collection.createIndex(
        Indexes.ascending("name"), collationIndexOptions);

Operation Level

The following operations support collation by specifying the Collation object to their respective Iterable object.

  • MongoCollection.aggregate()

  • MongoCollection.distinct()

  • MongoCollection.find()

  • MongoCollection.mapReduce()

For example:

Collation collation = Collation.builder()
                                 .collationStrength(CollationStrength.SECONDARY)
                                 .locale("fr").build();

collection.find(Filters.eq("category", "cafe")).collation(collation);

collection.aggregate(Arrays.asList(
                         Aggregates.group("$category", Accumulators.sum("count", 1))))
          .collation(collation);

The following operations support collation by specifying the Collation object as an option using the corresponding option class for the method:

  • MongoCollection.count()
  • MongoCollection.deleteOne()
  • MongoCollection.deleteMany()
  • MongoCollection.findOneAndDelete()
  • MongoCollection.findOneAndReplace()
  • MongoCollection.findOneAndUpdate()
  • MongoCollection.updateOne()
  • MongoCollection.updateMany()
  • MongoCollection.bulkWrite() for:

    • DeleteManyModel and DeleteOneModel using DeleteOptions to specify the collation
    • ReplaceOneModel, UpdateManyModel, and UpdateOneModel using UpdateOptions to specify the collation.

For example:

Collation collation = Collation.builder()
                                 .collationStrength(CollationStrength.SECONDARY)
                                 .locale("fr").build();

collection.count(Filters.eq("category", "cafe"), new CountOptions().collation(collation));

collection.updateOne(Filters.eq("category", "cafe"), set("stars", 1), 
                     new UpdateOptions().collation(collation));

collection.bulkWrite(Arrays.asList(
                new UpdateOneModel<>(new Document("category", "cafe"),
                                     new Document("$set", new Document("x", 0)), 
                                     new UpdateOptions().collation(collation)),
                new DeleteOneModel<>(new Document("category", "cafe"), 
                                     new DeleteOptions().collation(collation))));

For more information on collation, including the supported locales, refer to the manual.

Other MongoDB 3.4 features

Support for JNDI

The synchronous driver now includes a JNDI ObjectFactory implementation.

Upgrading

See the upgrading guide on how to upgrade to 3.5.