case class Prospect(
id_prospect: String, //UUID
projectUUID: String,
email: Option[String],
firstName: Option[String],
middleName: Option[String],
lastName: Option[String],
cookie: String,
leadStatus: LeadStatus,
visits: List[Visit],
temperature: Temperature,
notes: String,
favorite: Boolean,
assignee: Option[SwUser],
lastFavoriteNotificationDate: Option[DateTime] = None,
wasEverHot: Option[Boolean] = Some(false)
)
I'm trying to get some Stats using the Aggregation Framework.
Basically I want to get the number of Prospect per projectUUID given the leadStatus.
In MongoShell, I can do this: db.prospects.aggregate([{$group:{_id:"$projectUUID", totalLead:{$sum:{$add:1}}}}])
I created a case class:
case class ProspectStats(projectUUID: String, leadCount: Int)
object ProspectStats{
implicit val reader = Macros.reader[ProspectStats]
}
and a Aggregator Function:
def getProjectLeadStatsAggregate(col: BSONCollection, leadType: String): Future[List[ProspectStats]] = {
import col.BatchCommands.AggregationFramework.{
AggregationResult,
Group,
Match,
SumValue
}
val res: Future[AggregationResult] = col.aggregate(
Group(BSONString("$_id"))("totalLead" -> SumValue(1)))
res.map(_.result[ProspectStats])
}
But I keep getting a DocumentKeyNotFound exception on Macros.reader
[DocumentKeyNotFound: The key 'projectUUID' could not be found in this document or array]
case class ProspectStats(projectUUID: String, leadCount: Int)
32object ProspectStats{
33implicit val reader = Macros.reader[ProspectStats]
34}
List(Stream(Success((_id,BSONString(acab62ad-597e-4918-86bb-df3c85d4817a))), Success((totalLead,BSONInteger(1)))))
I'm using "org.reactivemongo" %% "play2-reactivemongo" % "0.11.7.play23", (play 2.3.9)
Do you know some other good code example that I could follow? Couldn't find much on gitHub.
Thanks a lot,
Olivier
There is documentation about aggregation: http://reactivemongo.org/releases/0.11/documentation/advanced-topics/aggregation.html
--
You received this message because you are subscribed to a topic in the Google Groups "ReactiveMongo - http://reactivemongo.org" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/reactivemongo/Dee6qJOkZak/unsubscribe.
To unsubscribe from this group and all its topics, send an email to reactivemong...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
case class ProspectStats(projectUUID: String, leadCount: Int)
object ProspectStats{
implicit object ProspectBSONReader extends BSONDocumentReader[ProspectStats] {
def read(doc: BSONDocument): ProspectStats ={
ProspectStats(
doc.getAs[BSONString]("_id").get.as[String],
doc.getAs[BSONInteger]("totalLead").get.as[Int]
)
}
}
}
Thanks a lot for your help!
Olivier
What's the error? As soon as there is a reader for T, you can use .result[T] on aggregation result.