Skip to content

v4.1.0

Choose a tag to compare

@shadowsmind shadowsmind released this 12 Oct 22:31
· 85 commits to main since this release

mongo4s 4.1.0

The aggregation vocabulary MongoDB added in 5.x–7.0 — the ranking and statistical accumulators, window functions,
$fill, $redact and the concise $lookup — plus bitwise updates and filters. Binary compatible with 4.0.0,
checked by MiMa with no exclusions. Upgrade notes:
COMPATIBILITY.md.

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

New features

  • Accumulators — top/bottom and topN/bottomN rank by a Sort and output a field; firstN/lastN,
    maxN/minN; median and percentile with the server's approximate method; stdDevPop/stdDevSamp;
    mergeObjects. The statistical ones compile only on a numeric field.
  • Window functions — WindowFunction.rank, denseRank, documentNumber, shift, locf, linearFill,
    derivative, integral, expMovingAvg/expMovingAvgAlpha, given to setWindowFields beside the accumulators.
    Putting one in a $group, where the server refuses it, does not compile.
  • $fill — by a value, by the last value seen, or linearly between neighbours, per partition, through
    FillOutput. The sort-driven methods are refused without a sortBy.
  • $redact, with Stage.Descend/Prune/Keep for the expression's answers.
  • Stage.lookupWithKeys — $lookup that joins on localField/foreignField and narrows with a sub-pipeline.
    Both keys are typed, so joining an ObjectId to a String is a compile error.
  • $bit — bitAnd/bitOr/bitXor — and the $bitsAllSet/$bitsAnySet/$bitsAllClear/$bitsAnyClear
    filters, on integer fields only through BitwiseOf.
  • Filter.jsonSchema.
  • The fake simulates the new accumulators except median/percentile, and $fill, $bit and the $bits
    filters — each pinned by a parity spec against MongoDB 8.2. Window functions, $redact, $jsonSchema,
    median and percentile throw UnsupportedOperationException naming what was asked for.

Bug fixes

  • The fake was not safe to share between fibers. Concurrent inserts lost documents or threw, a duplicate _id
    or unique key could get through twice, and concurrent $incs lost updates. Every operation now runs atomically,
    so a counter or a work queue claimed through findOneAndUpdate behaves under contention as it does against a
    server.

Benchmarks

Re-measured on 4.1.0 against mongo4cats 0.8.0, with the machine, server and every library version stated —
BENCHMARKS.md.

  • WireCodec to real BSON bytes: 2.4–3.2× faster than either mongo4cats codec, up to 6.4× less allocation.
  • Single-document operations against a real MongoDB land level for all six stacks.
  • Bulk reads pull ahead: mongo4cats allocates 2.1–6.0× more on findAll/findStream and reads findAll
    1.13–1.35× slower. Its find(...).stream on cats-effect runs at 1,626 ops/s against mongo4s's
    3,174 — 0.8.0 closed most of the gap 0.7.18 had (392 ops/s), not all of it.

Compatibility

  • mongodb-driver-reactivestreams 5.13.0; kyo 1.0.0-RC7. mongo4s-kyo remains outside the binary
    compatibility promise while kyo is on a release candidate.
  • Accumulator, Filter, Update and Stage have new cases, so an exhaustive match over one of them warns.
  • setWindowFields takes WindowFields, the common parent of WindowOutput and WindowFunction; every existing
    call compiles unchanged.
  • Scala 3.9 LTS; building requires JDK 25. Tested against JDK 25 and MongoDB 8.2.