For the most recent version of the reference documentation, see our MongoDB Java Driver documentation site.
- What's New
What’s New
This release includes full support for the upcoming 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
Decimal128.parse()
with a string:Decimal128.parse("9.9900");
new Decimal128()
with a long:new Decimal128(10L);
new Decimal128()
with ajava.math.BigDecimal
:new Decimal128(new BigDecimal("4.350000"));
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
andDeleteOneModel
usingDeleteOptions
to specify the collationReplaceOneModel
,UpdateManyModel
, andUpdateOneModel
usingUpdateOptions
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 specification of maximum staleness for secondary reads
- Support for the MongoDB handshake protocol.
- Builders for eight new aggregation pipeline stages
- Helpers for creating read-only views
- Support for the linearizable read concern
Support for JNDI
The synchronous driver now includes a JNDI ObjectFactory implementation.
Upgrading
See the upgrading guide on how to upgrade to 3.4.