James
James

Reputation: 2018

MongoDB is summing numbers incorrectly

I am using MongoDB to sum a number of transactions, however it is returning the wrong values.

All numbers are stored as a long - In mongo shell they appear as NumberLong("5")

I have spent some time looking at my data thinking I had collected it incorrectly, however I now realise MongoDB is not summing the values correctly.

My test data is here: http://pastebin.com/3LNQLFtR

The actual value I am looking for is 165516011841, however when I do a $sum aggregate in MongoDB it incorrectly reports 1641757914055.

Here is my aggregate:

{$project: {"value": '$o.v'}}
{$group: {"_id":1, "total":{$sum: "$value"}}}

Excel has the same issue by the way. The only way to sum it correctly is as they are intended, Int64.

As requested here is a example document: http://pastebin.com/BD4RuPyg

Edit, it appears that my data (and MongoDB) is correct. I was attempting to reach the Total Received value here: https://blockchain.info/address/1PEDJAibfNetJzM289oXsW1qLAgjYDjLgN

However it appears my number is more accurately a turnover of that particular value, and so better reflects what I am trying to present.

Upvotes: 0

Views: 677

Answers (2)

James
James

Reputation: 2018

Provided answer by MongoDB is correct, issue was with proof being incorrect:

https://blockchain.info/address/1PEDJAibfNetJzM289oXsW1qLAgjYDjLgN

Total Received on that page should be 1641757914055 not 165516011841.

Upvotes: 0

Maksym Strukov
Maksym Strukov

Reputation: 2689

Found your issue really interesting and decided to run some tests against the data you provided here http://pastebin.com/3LNQLFtR. There are two text areas on the page I used the data from the first one with 26926 records in it.

  • First I inserted the data into excel. Just copied and pasted the data column. Then selected all the values to look at the sum result.
  • Secondly I inserted it into a mongo collection. Using a simple document with one filed. db.testLong1.insert({v:NumberLong(499689200) }) Then run a group query, pretty much the same as you did.

    db.testLong1.aggregate({$project: {"value": "$v"}}, {$group:{"_id":1, "total":{$sum:"$value"}}})

  • Finally I inserted the data into a SQL Server db. Again a simple one field table.

    create table t1(v bigint not null)

    Inserted all the records with a number of insert statments, like this one

    insert t1 values('241025537')

    Then just ran this query to get the sum of the values

    select sum(v) from t1

All the tools gave me the same result, that is 1641757914055 How do you know this is wrong? I've been working with the tools for quite long and never heard about or experienced such an issue. So I tend to believe that it's not mongo that returns an incorrect result but a bug in your system which makes you believe this.

Hope it helps!

Upvotes: 1

Related Questions