push multiple fields in group (aggregation)

243 views
Skip to first unread message

Aakil Omar

unread,
Nov 5, 2017, 9:19:39 AM11/5/17
to Morphia
I am trying to do the following query in morphia

db.notification.aggregate({ $match: { customerId: { $in: [ 4860.0, 4861.0, 4862.0 ] } } },
{ $sort: { creationDate: -1.0 } },
{ $group: { _id: { group: "$notificationGroup", customer: "$customerId" }, count: { $sum: 1.0 }, notifications: { $push: "$$ROOT" } } },
{ $group: { _id: "$_id.group", customers: { $push: { customer: "$_id.customer", count: "$count", notifications: "$notifications" } } } })

the only issue I have is trying to push multiple fields into the "customers" value.

in the source I see that Group.push is defined as 

public static Accumulator push(final String field) {
return new Accumulator("$push", field);
}

also grouping cannot take another grouping, only a list of projections. 

Is there a way to achieve

customers: { $push: { customer: "$_id.customer", count: "$count", notifications: "$notifications" } }

from the current API ?

next step for me is to try and a push method that can take a list, but I am just wondering if I am not missing something obvious in API.

cloned master from git so should be looking at latest version.






Aakil Omar

unread,
Nov 7, 2017, 3:26:11 AM11/7/17
to Morphia
ok i created a patch to add this functionality, as it is a bit of a hack, it is probably not worth creating a branch for it and it does not look like anyone else need the functionality, could not find anyone requesting it.

I am sure someone that maintains this library would do it in a much cleaner way.
Accumulator.java
Group.java
Reply all
Reply to author
Forward
0 new messages