I have a document schema in a test collection like this:
{
"_id" : NumberLong("A unique number"),
"text" : {
"characters_map" : {
"a" : 4,
"f" : 3,
"b" : 6,
...
"o" : 3
}
...
}
...
}
I want to count the sum of each character in the whole collection. I tried using the aggregation framework, but it doesn't seem I do it correct.
I began by calculating each character separately ( i.e the 'a'), but with no luck. My best approach is:
> db.test.aggregate([
{ "$group" : {
_id : { a_count : "$text.characters_map.a" },
counter : { "$sum" : "$text.characters_map.a" }
}
}
])
with a_count representing the name of the id.
which results to:
{ "_id" : { "a_map" : 8 }, "counter" : 8 }
{ "_id" : { "a_map" : 5 }, "counter" : 5 }
{ "_id" : { "a_map" : 7 }, "counter" : 21 }
...
Not false entirely, but not what I want. These results mean I have 1 document with a field of: { "a" : 8, ... }, another 1 document with: { "a" : 5, ... } and 3 documents with: { "a" : 7, ... }.
Is there any way to group these fields together at "a" level? Is my approach completely wrong?