node.js 将大浮点数保存到 mongo 中进行搜索 [英] node.js saving large floating point numbers into mongo for searching

查看:78
本文介绍了node.js 将大浮点数保存到 mongo 中进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为自己有限的实际编程知识而苦苦挣扎,需要一些帮助...

I've been wrestling with my limited knowledge of real programming and need some help...

从 node.js 中并保存到 MongoDB(使用 本机驱动程序)我需要索引和搜索一些长数字.

From within node.js and saving into MongoDB (using the native driver) I need to index and search some long numbers.

示例:

87 / 53

在 64 位 Windows 计算器上,这是:

On 64-bit windows calculator this is:

1.6415094339622641509433962264151

我在 node.js 中运行它:

I run this in node.js:

var answer = 87 / 53;
console.log(answer);

我得到:

1.6415094339622642

我的理解是因为 node.js 只运行 32 位,我们没有得到所需的精度.

my understanding is because node.js only runs 32-bit, we don't get the precision needed.

因此,我插入了 node-bigint 库,该库包装了 libgmp 以进行大数数学运算使用字符串...

So, I plugged in the node-bigint library which wraps libgmp to do big number math using strings...

现在是:

var bigint = require('bigint');
var top = bigint('8700000000000000000000000000000000000');
var bottom = bigint(53);
var answer = top.div(bottom);
console.log(answer.toString());

我用零填充以删除浮点数(更容易 - 我只需要比较数字,即大于小于)...

I've padded with zeros to remove the floating point (easier - I only need to compare the numbers i.e. bigger than smaller than)...

此代码产生:

'164150943396226415094339622641509433'

太好了 - 我比 Windows 计算器更准确:)

Great - I've got more accuracy than the windows calculator : )

问题是,这个数字目前是一个字符串 - 我正在将字符串保存到 Mongo 并执行如下查询:

Problem is, this number is currently a string - I'm saving the string to Mongo and doing a query like:

{thenumber:{'$gt':'164150943396226415094339622641509433'}}

即我正在将字符串与字符串进行比较.这行得通,因为我已经对它们进行了相同的填充.

I.e. I'm comparing strings against strings. This works OK because I've padded them all the same.

问题是 - 这是存储和搜索值的最佳方式(即字符串)还是将它们转换为 64 位 Long 的索引和搜索速度更快...

The question is - is this the best way (i.e. strings) to store and search values or would converting them to 64 bit Long's be faster for indexing and searching...

如果答案是使用 Longs 它们更快",我终生无法(经过大量谷歌搜索)弄清楚如何将上面的字符串转换为原生 Mongo Long 数据类型(低位和高位) - 请帮帮他深度程序员:)

If the answer is 'Use Longs they are faster' I cannot for the life of me (after lots of Googling) figure out how to turn the string above into a native Mongo Long data type (with the low bits and high bits) - please help out an out of his depth programmer : )

跟进:

阅读长数据类型 使用此方法的驱动程序手册:

Reading Long DataType manual for the driver it has this method:

Long.fromBits(lowBits, highBits)

如此有针对性的问题 - 如何将代表 64 位整数的字符串转换为这些低位和高位以供 Long 保存...此外 - 这是否比字符串搜索更快?

So targeted question - how do I turn a string that represents a 64-bit integer into these low and high bits for the Long to save... Also - is this quicker to search than a string?

推荐答案

使用 Long.fromString 将字符串转换为 MongoDB Long:

Use Long.fromString to convert a string into a MongoDB Long:

var Long = require('mongodb').Long;
var long = Long.fromString(str, 10);

第二次编辑 - 实际上,对于包含有符号 64 位整数的字符串,这是 的正确答案.问题是您的 '164150943396226415094339622641509433' 字符串太大而无法存储为 64 位有符号整数.您可以使用的最大值是 (2^63 - 1) 或 9223372036854775807.

Second EDIT - Actually, this is the right answer for a string containing a signed 64-bit integer. The problem is that your string of '164150943396226415094339622641509433' is too large to be stored as a 64-bit signed integer. The largest value you can use is (2^63 - 1) or 9223372036854775807.

这篇关于node.js 将大浮点数保存到 mongo 中进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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