Reactive Scala Driver for MongoDB
Asynchronous & Non-Blocking
Raw Aggregation Stage
You can also implement aggregate stage using raw BSON (e.g. when there is no convenient stage yet provided), using the PipelineOperator
factory.
import scala.concurrent.ExecutionContext
import reactivemongo.api.bson._
import reactivemongo.api.bson.collection.BSONCollection
def customAgg(coll: BSONCollection)(implicit ec: ExecutionContext) =
coll.aggregateWith[BSONDocument]() { framework =>
import framework.PipelineOperator
val customStage = // { $sample: { size: 2 } }
PipelineOperator(BSONDocument("$sample" -> BSONDocument("size" -> 2)))
List(customStage)
}