How to store large numbers in MongoDB with Node.js

后端 未结 2 1603
谎友^
谎友^ 2020-12-18 23:26

I am using MongoDB with the native node driver and need to accurately store numbers that may be larger than the int maximum 2147483647. I will need to be able to increment t

相关标签:
2条回答
  • 2020-12-19 00:15

    MongoDB has a native 64-bit long integer type, usually called NumberLong (at least in the MongoDB javascript shell/driver). If you're using the node-mongodb-native driver, I believe it is just called Long (though I am not certain of this, someone please correct me if I'm wrong). $inc works as expected against fields of type NumberLong.

    0 讨论(0)
  • 2020-12-19 00:24

    You can now do this in Mongoose with the mongoose-long plug-in.

    require('mongoose-long')(mongoose);
    var SchemaTypes = mongoose.Schema.Types;
    var schema = new Schema({
        ...
        usedBandwidth: {type: SchemaTypes.Long, min: 0, default: 0}
    });
    
    0 讨论(0)
提交回复
热议问题