Someone asked
In SQL: Select sum (price) from ordersin Mongo: DB. users. aggregate ([{$ group: {_ ID: NULL, total: {$ sum: "$ price" }}]) in SQL: Select sum (price) from ordershaving sum (price)> 10 in Mongo :?
Mapreduce can be implemented, but it is not worth it. BecauseCodeIt is too tedious to write.
In fact, We Can slightly change the order of $ match and $ group in aggregation framework.
DB. t4.insert ({"_ id": 1, "name": "apple", "price": 10}) dB. t4.insert ({"_ id": 2, "name": "boy", "price": 5}) dB. t4.insert ({"_ id": 3, "name": "apple", "price": 20}) dB. t4.insert ({"_ id": 4, "name": "apple", "price": 30}) dB. t4.insert ({"_ id": 5, "name": "Cup", "price": 10}) dB. t4.insert ({"_ id": 6, "name": "Cup", "price": 20}) dB. t4.insert ({"_ id": 7, "name": "dark", "price": 10}) dB. t4.insert ({"_ id": 8, "name": "dark", "price": 10}) dB. t4.insert ({"_ id": 9, "name": "egg", "price": 10}) dB. t4.insert ({"_ id": 0, "name": "film", "price": 10}) dB. t4.aggregate ([{$ group: {_ ID: "$ name", total: {$ sum: "$ price" }},{$ match: {"Total ": {"$ gt": 10 }}])
Of course, I have also tested the traditional $ group for processing in finalize. However, the problem is that the finalize function operation object is the internal elements of the array. Deleting the finalize function will not change the size of the result set.
My code is as follows:
DB. t4.group ({key: {"name": true}, reduce: function (OBJ, Prev) {Prev. psum + = obj. price;}, initial: {psum: 0}, finalize: function (out) {If (Out. psum <= 10) return NULL ;}});