Skip to content

v5.0.0

Choose a tag to compare

@shadowsmind shadowsmind released this 26 Oct 15:17
· 61 commits to main since this release

mongo4s 5.0.0

Typed aggregation expressions, Atlas Search, the client-level bulkWrite, typed positional updates, conditional
writes and counters on the repository, and a fake that simulates upserts. A major release: several methods gained
parameters and several AST cases changed shape, so anything compiled against 4.x has to be recompiled, though most
calls compile unchanged. Migration notes:
COMPATIBILITY.md.

libraryDependencies += "org.mongo4s" %% "mongo4s-core" % "5.0.0"

New features

  • Typed aggregation expressions — Expr[E, A] computes a value from a document: field.expr, Expr.lit,
    arithmetic, comparisons, logic, cond/switch, text, array (filter/map over a typed element), date and
    conversion operators, each compiling only where the server accepts it. They go into $addFields
    ("total" := price.expr * qty.expr), $group keys and accumulators, $replaceWith, $redact, computed
    projections in find and $project, Filter.expr and pipeline updates.
  • Atlas Search — $search and $searchMeta over typed SearchOperators (text, phrase, autocomplete,
    wildcard, regex, equalTo, ranges, exists, compound), $vectorSearch approximate or exact with a Filter
    pre-filter, search-index management with typed vector indexes, and Expr.searchScore. Tested against a
    mongodb-atlas-local container.
  • Client-level bulkWrite (MongoDB 8.0+) — collection.write(command) addresses a WriteCommand with that
    collection's own codec, and client.bulkWrite sends writes to several collections and databases in one command.
  • Typed positional updates — field.each ($[]), field.matched ($) and field.where(element) ($[id]),
    with Field.element building both the path and its array filter.
  • pullWhere removes array elements by a condition; Field.self states a condition on a scalar element.
  • Repository writes — insertIfAbsent(entity): F[Boolean] inserts only when the key is free, without
    overwriting and without failing a transaction; replace(entity, condition) replaces by key only while the
    condition holds, for optimistic concurrency.
  • Counters — MongoCounters.create(database, name) gives next, current and reset over one document per
    counter, next being a single atomic findOneAndUpdate; FakeCounters in the testkit.
  • WireCodecConfig.withDecodeMissingAsDefault — fill a missing field from the case-class default, for documents
    written before the field existed. Off by default.
  • Options — InsertOneOptions/InsertManyOptions (InsertManyOptions.unordered inserts past a duplicate),
    BulkWriteOptions, let variables on every write and on find/aggregate, sort on updateOne/replaceOne
    (8.0+), tailable cursors streamed live, maxAwaitTime, noCursorTimeout, allowDiskUse, hint/comment on
    distinct, EstimatedCountOptions.
  • Smaller additions — $type over several types (hasAnyType), $currentDate as a timestamp, $unwind's
    includeArrayIndex, $unionWith with a pipeline, text-index weights and languages.
  • The fake simulates update-based upserts, building the inserted document from the filter the way the server
    does, plus unordered inserts, a sort on single-document updates, pullWhere, conditions on scalar elements,
    $type over several types and a timestamp $currentDate — each pinned by a parity spec against MongoDB 8.2.

Breaking changes

  • insertOne/insertMany take options, and bulkWrite takes BulkWriteOptions instead of ordered.
  • Stage.addFields takes computed fields — "name" := expr — instead of (String, BsonValue) pairs.
  • Stage.Group, Stage.Redact, Filter.Expr, Filter.Type, Update.CurrentDate, Projection.Include,
    Stage.Unwind, Stage.UnionWith and WriteCommand.DeleteOne/DeleteMany changed shape.
  • FindQuery, AggregateQuery, DistinctQuery, MongoCollection, MongoClient and Repository have new abstract
    members.
  • containsAll(Nil) matches nothing, as $all: [] does on the server.
  • A sort on updateMany is refused rather than dropped by the driver.

Bug fixes

  • ensureKeyIndex failed on every repository keyed on _id; it now builds nothing for such a key and answers _id_.
  • Slicing a field the projection also included returned every field of the document.
  • A window over Accumulator.raw wrote into the document it was given, so reusing it in a $group failed.
  • A failed transaction commit carried a misleading suppressed IllegalStateException.
  • The fake wrote a positional path as a field called $[], accepted a unique index on _id and partial indexes the
    server refuses, matched documents on $all: [], and gave a replacement upsert a new _id instead of the
    filter's.

Compatibility

  • Scala 3.9 LTS; class files target Java 17, mongo4s-kyo needs JDK 25 at runtime for kyo's sake, and building
    requires JDK 25. Tested against MongoDB 8.2.
  • mongodb-driver-reactivestreams 5.13.0, zio 2.1.27, kyo 1.0.0-RC7. mongo4s-kyo remains outside the
    binary compatibility promise while kyo is on a release candidate.
  • The build fails on any compiler warning.