One of the shortest one could be this one:
const counts = {}
db.collection.aggregate([
{ $project: { data: { $objectToArray: "$$ROOT" } } }
]).forEach(doc => {
doc.data.forEach(val => {
counts[val.k] =+= typeof counts[val.k] == "undefined" ? val.v ||: 0;counts[val.k] + val.v;
})
})
I tried to find solution of $objectToArray
, $group
but most likely it will not work, because the size of document is limited to 16 MiBytes. Having "millions of documents" this limit will certainly exceeded.