如何使用Node.js在MongoDB中存储大量数据
我正在使用MongoDB的本地节点驱动程序,并需要准确地存储可能大于最大2147483647 int的数字。我将需要能够增加数字,因为我正在跟踪使用情况。 我有什么select?
我正在使用mongoose。 我遇到的问题是,当我$inc
超过2147483647它将数字转换为一倍。 我如何强制Mongoose使用64位long 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} });
MongoDB有一个本机的64位长整数types,通常被称为NumberLong
(至less在MongoDB javascript shell / driver中)。 如果你使用node-mongodb-native驱动,我相信它只是叫做Long
(虽然我不确定这个,如果我错了,请指正)。 $inc
按照预期对NumberLong
types的字段进行NumberLong
。