1

I'm trying to convert the following mongo query to Reactive Mongo Equivalent ( JSON )

db.media.aggregate(   {$group : { "_id" : "$createdBy", "count" : { $sum : 1 }}},   {$sort : {"count" : -1}},   {$limit : 10} )

What I have come up with is this, but can't get around it.

override def getMediasCountByHandle(db:reactivemongo.api.DefaultDB): Future[JsObject] = {
    val commandDoc = Json.obj(
    "aggregate" -> "media", // we aggregate on collection orders
    "pipeline" -> List(
      Json.obj(
        "$group" -> Json.obj(
          "_id" -> "$createdBy",
          "count" -> Json.obj("$sum" -> 1))),
      Json.obj("$sort" -> Json.obj("total" -> -1)),
      Json.obj("$limit" -> 10)
    )
  )
  val runner = Command.run(JSONSerializationPack)

  runner.apply(db, runner.rawCommand(commandDoc)).one[JsObject]
}

Please help

cchantep
  • 9,118
  • 3
  • 30
  • 41

0 Answers0