如何使用 Node.js 在 MongoDB 中存储大量数字 [英] How to store large numbers in MongoDB with Node.js

查看:39
本文介绍了如何使用 Node.js 在 MongoDB 中存储大量数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 MongoDB 与本机节点驱动程序一起使用,并且需要准确存储可能大于 int 最大值 2147483647 的数字.我需要能够在跟踪使用情况时增加数字.我有哪些选择?

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 the number as I am tracking usage. What are my options?

我正在使用猫鼬.我遇到的问题是,当我 $inc 超过 2147483647 时,它会将数字转换为双精度数.如何强制 Mongoose 使用 64 位 long int NumberLong?

I am using Mongoose. The problem I am having is that when I $inc past 2147483647 it converts the number to a double. How can I force Mongoose to use the 64-bit 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 在 Mongoose 中执行此操作-long 插件.

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}
});

这篇关于如何使用 Node.js 在 MongoDB 中存储大量数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆