ReactiveMongo/reactivemongo-site

Documentation about the aggregate operation

Closed this issue · 0 comments

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

import reactivemongo.bson._
import reactivemongo.api.collections.bson.BSONCollection

def populatedStates(cities: BSONCollection): Future[List[BSONDocument]] = {
  import cities.BatchCommands.AggregationFramework
  import AggregationFramework.{ Group, Match, SumField }

  cities.aggregate(Group(BSONString("$state"))(
    "totalPop" -> SumField("population")), List(
    Match(document("totalPop" ->
      document("$gte" -> 10000000L))))).map(_.documents)
}