I have a fairly complex document model that is structurally like this:
{
_id: 1,
"title": "I'm number one",
... (many other meta data text fields not desired in the summary)
"foo": {
"tom": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
"dick": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
"harry": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
... (Total of 14 fields in foo)
},
"bar": {
"joe": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
"fred": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
"bob": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
... (Total of 14 fields in bar)
},
"dodads": [
{
"contraption": 0,
"doohickey": 0,
"gewgaw": 0,
"gizmo": 0,
... (total of 15 elements in each doodad object)
},
{
"contraption": 0,
"doohickey": 0,
"gewgaw": 0,
"gizmo": 0,
...
},
... (total of 6 objects in dodads object array)
]
},
... (a couple hundred documents in total)
What I'm looking for is a summary of all the objects/arrays that have numeric data. I would like the result to be a document, in the original format, that contains the numeric fields summarized. For now, let's say the documents all have the same structure.
The aggregation result would be like the following
{
"foo": {
"tom": [35, 65, 13, 22, 36, 58, 93, 43, 56, 44, 23, 72],
"dick": [56, 87, 28, 49, 34, 22, 48, 86, 29, 23, 88, 29],
... (All 14 fields in foo)
},
"bar": {
"joe": [87, 28, 49, 34, 22, 48, 86, 29, 23, 88, 29, 47],
"fred": [13, 22, 36, 58, 93, 43, 56, 44, 23, 72, 35, 65],
... (All 14 fields in bar)
},
"dodads": [
{
"contraption": 45,
"doohickey": 88,
"gewgaw": 23,
"gizmo": 64,
... (All 15 elements in each doodad object)
},
{
"contraption": 12,
"doohickey": 73,
"gewgaw": 57,
"gizmo": 86,
...
},
... (All 6 objects in dodads object array)
]
}
I believe I can unwind the arrays, specify sums and projections and get exactly what I want with an extensive and verbose aggregation pipeline. I could also do multiple queries grabbing the component pieces (one that's just foo, a second that's just bar...).
What I'm wondering is, is there a shorthand way of specifying summarizations? For example, can I say I want the summary of foo
or foo.tom
and get back their contents summarized?