MongoDB - Aggregate Sum

前端 未结 1 1220
醉梦人生
醉梦人生 2021-01-13 20:10

I am attempting to calculate the total amount of money spent being tracked inside of our database. Each order document contains a field \"total_price\"

I am attempti

相关标签:
1条回答
  • 2021-01-13 20:56

    $sum only works with ints, longs and floats. Right now, there is no operator to parse a string into a number, although that would be very useful. You can do this yourself as is described in Mongo convert all numeric fields that are stored as string but that would be slow.

    I would suggest you make sure that your application stores numbers as int/long/float, and that you write a script that iterators over all your documents and updates the value. I would also suggest that you add a feature request at https://jira.mongodb.org/browse/SERVER to add an operator that converts a string to a number.

    0 讨论(0)
提交回复
热议问题