Repository navigation
v5.0.0
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/mapover a typed element), date and
conversion operators, each compiling only where the server accepts it. They go into$addFields
("total" := price.expr * qty.expr),$groupkeys and accumulators,$replaceWith,$redact, computed
projections infindand$project,Filter.exprand pipeline updates. - Atlas Search —
$searchand$searchMetaover typedSearchOperators (text,phrase,autocomplete,
wildcard,regex,equalTo, ranges,exists,compound),$vectorSearchapproximate or exact with aFilter
pre-filter, search-index management with typed vector indexes, andExpr.searchScore. Tested against a
mongodb-atlas-localcontainer. - Client-level
bulkWrite(MongoDB 8.0+) —collection.write(command)addresses aWriteCommandwith that
collection's own codec, andclient.bulkWritesends writes to several collections and databases in one command. - Typed positional updates —
field.each($[]),field.matched($) andfield.where(element)($[id]),
withField.elementbuilding both the path and its array filter. pullWhereremoves array elements by a condition;Field.selfstates 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)givesnext,currentandresetover one document per
counter,nextbeing a single atomicfindOneAndUpdate;FakeCountersin 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.unorderedinserts past a duplicate),
BulkWriteOptions,letvariables on every write and onfind/aggregate,sortonupdateOne/replaceOne
(8.0+), tailable cursors streamed live,maxAwaitTime,noCursorTimeout,allowDiskUse,hint/commenton
distinct,EstimatedCountOptions. - Smaller additions —
$typeover several types (hasAnyType),$currentDateas a timestamp,$unwind's
includeArrayIndex,$unionWithwith 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, asorton single-document updates,pullWhere, conditions on scalar elements,
$typeover several types and a timestamp$currentDate— each pinned by a parity spec against MongoDB 8.2.
Breaking changes
insertOne/insertManytake options, andbulkWritetakesBulkWriteOptionsinstead ofordered.Stage.addFieldstakes computed fields —"name" := expr— instead of(String, BsonValue)pairs.Stage.Group,Stage.Redact,Filter.Expr,Filter.Type,Update.CurrentDate,Projection.Include,
Stage.Unwind,Stage.UnionWithandWriteCommand.DeleteOne/DeleteManychanged shape.FindQuery,AggregateQuery,DistinctQuery,MongoCollection,MongoClientandRepositoryhave new abstract
members.containsAll(Nil)matches nothing, as$all: []does on the server.- A
sortonupdateManyis refused rather than dropped by the driver.
Bug fixes
ensureKeyIndexfailed 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.rawwrote into the document it was given, so reusing it in a$groupfailed. - A failed transaction commit carried a misleading suppressed
IllegalStateException. - The fake wrote a positional path as a field called
$[], accepted auniqueindex on_idand partial indexes the
server refuses, matched documents on$all: [], and gave a replacement upsert a new_idinstead of the
filter's.
Compatibility
Scala 3.9 LTS; class files target Java 17,mongo4s-kyoneeds JDK 25 at runtime for kyo's sake, and building
requires JDK 25. Tested against MongoDB 8.2.mongodb-driver-reactivestreams5.13.0,zio2.1.27,kyo1.0.0-RC7.mongo4s-kyoremains outside the
binary compatibility promise while kyo is on a release candidate.- The build fails on any compiler warning.