Vote count: 0
I have been struggling with this for hours, and I'm sure this will be easy for some of you out there.
I have a collection with 2 topmost arrays:
{
...,
topmost1: ["123", "456"],
topmost2: ["abc","xyz"],
...
}
I need to aggregate counts for each where there are multiples. I've tried $unwind on both with $sum, using $size on one, but can't get the logic right.
I got this no problem: unwind one sum into a count, check for multiples then sort from most at the top to least at the bottom. But I'm having trouble with the compounds $groups and/or $unwinds. Here's the pipeline for one.
[
{ '$unwind' : '$topmost1' },
{ '$group' : { '_id' : '$topmost1', 'count': { '$sum':1 } } },
{ '$match' : { 'count' : { '$gt': 1 } } },
{ '$sort' : { 'count' : -1 } }
]
The end result should show show all records that have more than one topmost1 and topmost2 as well as their values. So I wouldn't include information for either if either one were empty.
Thanks for any help!
asked 57 secs ago
mongodb aggregation for 2 topmost arrays with counts
Aucun commentaire:
Enregistrer un commentaire