Skip to main content
added 56 characters in body
Source Link

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.

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] =+ val.v || 0;
   })
})

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.

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 : 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.

Source Link

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] =+ val.v || 0;
   })
})

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.