Frage

I'm trying to implement an aggregate method in ReactiveMongo but I'm a bit stuck.

I have the following dataset:

{
    "_id" : ObjectId("522891aa40ef0b5d11cb9232"),
    "created" : 1378390442167,
    "origin" : 2,
    "originIpAddress" : "",
    "rating" : 3,
    "remindersSent" : 1,
    "status" : 4,
    "text" : "",
    "updated" : 1378563426223,
    "userInfo" : {
        "firstName" : "Person",
        "lastName" : "Person",
        "email" : "person@person.com",
        "fbPublish" : false
    },
    "venueInfo" : {
        "isAgent" : false,
        "name" : "Company",
        "id" : 1234
    }
},
{
    "_id" : ObjectId("522891aa40ef0b5d11cb9233"),
    "created" : 1378390442167,
    "origin" : 2,
    "originIpAddress" : "",
    "rating" : 3,
    "remindersSent" : 1,
    "status" : 4,
    "text" : "",
    "updated" : 1378563426223,
    "userInfo" : {
        "firstName" : "Person2",
        "lastName" : "Person2",
        "email" : "person2@person.com",
        "fbPublish" : false
    },
    "venueInfo" : {
        "isAgent" : false,
        "name" : "Company2",
        "id" : 4321
    }
},
{
    "_id" : ObjectId("522891aa40ef0b5d11cb9234"),
    "created" : 1378390442167,
    "origin" : 2,
    "originIpAddress" : "",
    "rating" : 3,
    "remindersSent" : 1,
    "status" : 4,
    "text" : "",
    "updated" : 1378563426223,
    "userInfo" : {
        "firstName" : "Person3",
        "lastName" : "Person3",
        "email" : "person3@person.com",
        "fbPublish" : false
    },
    "venueInfo" : {
        "isAgent" : false,
        "name" : "Company",
        "id" : 1234
    }
}

The following aggregate function:

db.reviews.aggregate(
    {$match:{status:{"$ne":1}}},
    {$group: { _id: "$venueInfo.id", total:{"$sum":1}}}
)

gives me:

{
    "result" : [ 
        {
            "_id" : 1234,
            "total" : 2
        }, 
        {
            "_id" : 4321,
            "total" : 1
        }
    ]
}

I tried to implement this in ReactiveMongo:

def aggregate() = {
    val command = Aggregate(collection.name, Seq(
      GroupField("venueInfo.id")("total" -> SumValue(1)),
      Match(BSONDocument("status" -> 1))
    ))
    val result = collection.db.command(command)
      result.map { value => {
        println(s"got value $value")
      }

  }

And that gives me:

got value Stream(BSONDocument(<non-empty>), ?)

As you can see I get a Stream back. So my question is: How do I handle this stream in a correct way so that I can work with the values and show them later in the view?

War es hilfreich?

Lösung

If you would like to retrieve all the values of the given Stream you can invoke toSeq or toList on it:

import play.modules.reactivemongo.json.BSONFormats._
import SomeResult

collection.db.command(command) map { result =>
  result.toSeq map (Json.toJson(_).as[SomeResult])
}

which results in a Future[Seq[SomeResult]], where SomeResult would be a case class like the following:

import play.api.libs.json.Json

case class SomeResult(_id: Long, total: Int)

object SomeResult {
  implicit val someResultFormat = Json.format[SomeResult]
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top