Clicky

Mongo - Sum values inside documents

August 17, 2016

Schema:
<![CDATA[ { (…) “offers” : { “offerCount” : 1, (…) } ]]>
SyntaxHighlighter.highlight();

Solution:

<![CDATA[ db.getCollection(‘<collection_name>’) .aggregate( {$group:{_id:"", totalAmount: { $sum: “$offers.offerCount” }, count: { $sum: 1 }}} ) ]]>
SyntaxHighlighter.highlight();
Note: schema based on https://schema.org/AggregateOffer