1

I'm using the mongodb 2.2.33 library under nodejs (x64) and am having trouble incrementing a field:

  it('increment-test', function () {
    let db = repository.db;
    return setup
      .then(() => {
        return db.collection('product').save({ name: 'product1', amount: 0 });
      })
      .then(() => {
        return db.collection('product').update({}, 
                   { $inc: { "amount": mongo.Long(5000000000000000) } });
      })
      .then(() => {
        return db.collection('product').find({}).toArray();
      })
      .then((results) => {
        assert.equal(results[0].amount, 5000000000000000);//fails
      })

  });

This test fails; instead of 5000000000000000 you actually get 937459712

How can i get this to work under NodeJS? mongo.Long appears to be the correct type to use from NodeJS but doesnt work. In case you were wondering the above test passes when you use another number bigger than Int32 - e.g. 3147483647 The largest number this passes with is 4294967296 which is 2^32 -1

wal
  • 17,409
  • 8
  • 74
  • 109
  • That's JavaScript mate. Which is why the `Long` accepts a "string" as an argument. `NumberLong("5000000000000000")`. Or just type in `NumberLong("5000000000000000") / 2` to be more succinct. People thought about this long before you came across it. – Neil Lunn Nov 13 '17 at 20:16
  • @NeilLunn why do you refer to`NumberLong` when the issue refers to `mongo.Long` ? nb, `mongo.Long("5000000000000000")` also does not fix problem – wal Nov 14 '17 at 00:22
  • There's a [`fromString()`](http://mongodb.github.io/node-mongodb-native/2.2/api/Long.html#.fromString), I actually near linked that in the original comment but "your question" shows `NumberLong()` as it is viewed from the shell. But it's the same implementation in ALL drivers, and it's pretty well documented as well. – Neil Lunn Nov 14 '17 at 00:30
  • Related: [MongoDB differences between NumberLong and simple Integer?](https://stackoverflow.com/questions/17185220/mongodb-differences-between-numberlong-and-simple-integer) – Neil Lunn Nov 14 '17 at 00:32
  • @NeilLunn can you please explain what is wrong with "my question" – wal Nov 14 '17 at 00:37
  • What do you mean "what's wrong"? You used `Long(5000000000000000)` and you are meant to use `Long.fromString("5000000000000000")` or `high` and `low` in the constructor, whatever "floats your boat". It is actually [there in the documentation](http://mongodb.github.io/node-mongodb-native/2.2/api/Long.html). All I see is that you are asking something that indicates you did not look at the documentation. So I'm showing you where the documentation is. Somehow I have this strange view that this allows you to continue and resolve the issue. – Neil Lunn Nov 14 '17 at 00:46
  • @NeilLunn please add as an answer if you wish – wal Nov 14 '17 at 00:55

0 Answers0