我将MongoDB与 native Node 驱动程序一起使用,需要准确存储可能大于int最大值2147483647的数字。在跟踪使用情况时,我将需要能够增加数字。我有什么选择?

我在用 Mongoose 。我遇到的问题是,当我$inc经过2147483647之后,它将数字转换为 double 数。如何强制Mongoose使用64位长的int NumberLong

我的架构看起来像这样

var schema = new Schema({
    ...
    usedBandwidth: {type: Number, min: 0, default: 0}
});

我的$inc看起来像这样
model.Account.update(
    {_id: this.account},
    {$inc: {usedBandwidth: this.size}},
    function (err, doc) {}
);

最佳答案

现在,您可以使用 mongoose-long 插件在Mongoose中执行此操作。

require('mongoose-long')(mongoose);
var SchemaTypes = mongoose.Schema.Types;
var schema = new Schema({
    ...
    usedBandwidth: {type: SchemaTypes.Long, min: 0, default: 0}
});

关于node.js - 如何使用Node.js在MongoDB中存储大量数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8617272/

10-09 22:17