reactivemongo extension json aggregate

100 views
Skip to first unread message

David Sanchez

unread,
Oct 8, 2015, 3:37:46 PM10/8/15
to ReactiveMongo - http://reactivemongo.org
Hi, 

Is there any tuto on how using aggregate with json reactivemongo extension 0.11.7.play23 ?

Assuming my pseudo following documents:
{ date: 46402320, userid: ObjectID("1"), valueA: "u1vA1", valueB: "u1vB1" }
{ date: 46402321, userid: ObjectID("1"), valueA: "u1vA2", valueB: "u1vB2" }
{ date: 46402322, userid: ObjectID("1"), valueA: "u1vA3", valueB: "u1vB3" }
{ date: 46402340, userid: ObjectID("2"), valueA: "u2vA1", valueB: "u2vB1" }
{ date: 46402341, userid: ObjectID("2"), valueA: "u2vA2", valueB: "u2vB2" }
{ date: 46402342, userid: ObjectID("2"), valueA: "u2vA3", valueB: "u2vB3" }

I want to get for a given userid (for example ObjectID("1")) sorted by ascending date the following result:

{
t
: [46402320, 46402321, 46402322, ...],
va
: ["u1vA1", "u1vA2", "u1vA3", ...],
vb
: ["u1vB1", "u1vB2", "u1vB3", ...]
}

where t is the array of date of user "1"
va the array of valueA of user "1"
vb the array of valueB of user "1"

Thanks !



David Sanchez

unread,
Oct 9, 2015, 3:29:29 AM10/9/15
to ReactiveMongo - http://reactivemongo.org
Ok I found a solution so I share it :

    import play.modules.reactivemongo.json.commands.JSONAggregationFramework._


    collection
.aggregate(Match("userid" $eq userId),
     
List(Group(JsNull)(
       
"t" -> Push("date"),
       
"va" -> Push("valueA"),
       
"vb" -> Push("valueB")
     
))
   
)

That's all !
Reply all
Reply to author
Forward
0 new messages